一对多关系中的核心数据排序
我已经设置了一个数据模型,其中学生实体的名称与主题具有一对多关系。他参加的每个科目都有多个上课时间。
下面的代码根据学生姓名对其进行排序,这很简单。
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];
我想做的就是对其进行排序,以便在每个学生需要参加的下节课时间进行排序。因此,显示将为“学生”,并按下一堂课的时间排序。有什么想法吗?
我尝试了以下操作,
[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];
但收到错误“'此处不允许对多密钥'”,然后我尝试了 setRelationshipKeyPathsForPrefetching: 但没有获取主题的对多关系。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
过程是这样的:
试试这个。接学生。然后,对于每个学生,从班级中获取“时间”,为学生获取“大于或等于当前时间”(以谓词格式),按“时间”排序。显示详细信息。如果你把它放在一个循环中,你就会得到答案。
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.