开发者

Core Data sorting in one to many relationship

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:[N开发者_StackOverflowSPredicate 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?


This is the process:

Try this. Fetch Students. Then, for each student, fetch 'time' from Class, 'greater than or equal to current time' for Student (in predicate format), sorted by 'time'. Display details. If you put this in a loop, you have your answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜