0

I am trying to pull all the Actions which belong to the latest Session.

In SQL this would be a simple JOIN ... WHERE Session.startTime = MAX(Session.startTime), but with Core Data I can't figure out how to do this without resorting to two steps as below. Any ideas?

// Step 1. Get the latest session

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext]];
[request setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"startTime" ascending:NO]]];
[request setFetchLimit:1];

Session *lastSession = nil;

NSArray *objects = [self.managedObjectContext executeFetchRequest:request error:NULL]; 

if ([objects count] > 0)
{ 
    NSLog(@"max: %@", [objects objectAtIndex:0]);
    lastSession = [objects objectAtIndex:0];
}

// Step 2. Get that session's actions

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Action" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sectionSort = [[NSSortDescriptor alloc] initWithKey:@"session.startTime" ascending:NO selector:nil];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO selector:nil];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sectionSort, sort, nil]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"session.startTime = %@", lastSession.startTime];
//**Can't I do something like this instead?**
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"session.startTime = session.@max.startTime"];
[fetchRequest setPredicate:predicate];

[fetchRequest setFetchBatchSize:10];

[self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"session.startTime" cacheName:@"ActionHistory"];
self.fetchedResultsController.delegate = self;

NSError *error;
if (![self.fetchedResultsController performFetch:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();  // Fail
}

Edit: I do need to stick with using NSFetchedResultsController due reasons not fully detailed above.

trapper
  • 11,716
  • 7
  • 38
  • 82
  • Hope this helps: [http://stackoverflow.com/questions/6515167/using-min-max-and-etc-inside-a-predicate][1] [1]: http://stackoverflow.com/questions/6515167/using-min-max-and-etc-inside-a-predicate – Will Johnston May 24 '12 at 09:58
  • That is for filtering an array though, I want to filter a fetch request – trapper May 24 '12 at 10:14

1 Answers1

0

I would advise you to make a relationship beetwen Session and Action. Then, after you fetch the session you need, you could get all that session's actions as easy ss lastSession.actions.

Edit Maybe you can do

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"session = %@",lastSession];
Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66
  • I do have a relationship between these two already. But by doing it like that, I couldn't use all the benefits of `NSFetchedResultsController's` anymore – trapper May 25 '12 at 09:25
  • But I will still need to get the lastSession separately first before I could use that predicate though – trapper May 25 '12 at 09:45