0

I've been making a program that uses core data. It has two entities, Medicine and Log medicines have a set of logs

I have it adding a log to a medicine when its first added. this log includes a date. I want to show the latest date (there can be more than one log) for the medicine but don't know how to retrieve the appropriate log. Tutorials i've found only show how to add but not retrieve.

This is what i have so far, it displays the medicine name. i want to change the detailtextlabel to show the date of the latest log entry

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"%@",object);
    cell.textLabel.text = [object valueForKey:@"name"];
    cell.detailTextLabel.text = [[object valueForKey:@"active"] stringValue];
}

- (NSFetchedResultsController *)fetchedResultsController
    {
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *medicines = [NSEntityDescription entityForName:@"Medicines" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:medicines];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:NO];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
} 
Ceri Turner
  • 830
  • 2
  • 12
  • 36
  • Could you provide some details about your *fetchedResultsController*? – Lorenzo B Jun 11 '12 at 14:56
  • At the moment its only getting medicines. I've read some pages that say i need to also get the second table the same way, and others that just say i can get the using the nsset log – Ceri Turner Jun 11 '12 at 15:18

1 Answers1

0

You give very little details on your setup, so it's tough to give you a detailed answer.

Let's tell you have something like that:

Medicine:
- name: string
- logs: ->> Log

Log:
- medicine: -> Medicine
- date: NSDate

At some point, you have a NSFetchRequest. It should look like this:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName: @"Log"];
fetchRequest.predicate = [NSPredicate predicateWithFormat: ...

Now the magic starts.

fetchRequest.sortDescriptors = @[
    [NSSortDescriptor sortDescriptorWithKey: @"date" ascending: NO]
];

The first entry in the resulting set will be the most recent. 'fetchedLogs'.firstObject.date will return the most recent log entry date.

As I said, I cannot give you a more tailored answer as your question wasn't very detailed.

edit: Oh, you added some details while I was writing this. All right, I do not know how the relationship between medicines and log entries looks like. Probably something close to what I proposed above. So all you need is:

[medicine.logs valueForKeyPath: @"@max.date"];

This will return the highest date in the logs set.

edit: oops, my bad, valueForKeyPath, not just valueForKey.

  • thats pretty much the setup i have. at the moment the only important fields are: Medicine: name - string logs - nsset – Ceri Turner Jun 11 '12 at 15:43
  • I should add @max.date is the easiest way to get what you need, but certainly not the fastest. A tailored NSFetchRequest will be much faster if an index exists for Log.date. – fabrice truillot de chambrier Jun 11 '12 at 15:51
  • The Medicine has a nsset of Log " property (nonatomic, retain) NSSet *log; " Log has a Medicine " property (nonatomic, retain) Medicines *medicine; " – Ceri Turner Jun 11 '12 at 15:57
  • ok, i assume i'm getting this because i've not retrieved any logs (or at least thats what internet searches tell me) I get it when trying to print out the details of "object" 2012-06-11 17:29:12.379 Medicine Tracker[10766:fb03] Relationship 'log' fault on managed object (0x6d33990) (entity: Medicines; id: 0x6d32380 ; data: { active = 1; amountToTake = 1; hoursBetween = 1; log = ""; name = Med; }) – Ceri Turner Jun 11 '12 at 16:30