I have setup a data model where Student Entity has a name a 1-to-many relationship with Subject. Each Subject that he attends has a number of Class Times.
The code below sorts it, based on the Student name, this is straight forward.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
What I want to do is sort it so that it is sorted on next Class time each Student needs to attend. So the display will be Student and sorted on the time of the next class. Any ideas?
I've tried the following
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Student" inManagedObjectContext:managedObjectContext]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(subjects.time > %@)", [NSDate date]]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
I get the error "'to-many key not allowed here'", then I tried setRelationshipKeyPathsForPrefetching: but that didn't fetch the to-many-relationship of subjects. Any ideas?