一对多关系中的核心数据排序

发布于 2024-09-25 21:35:22 字数 1249 浏览 3 评论 0原文

我已经设置了一个数据模型,其中学生实体的名称与主题具有一对多关系。他参加的每个科目都有多个上课时间。

下面的代码根据学生姓名对其进行排序,这很简单。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-10-02 21:35:22

过程是这样的:

试试这个。接学生。然后,对于每个学生,从班级中获取“时间”,为学生获取“大于或等于当前时间”(以谓词格式),按“时间”排序。显示详细信息。如果你把它放在一个循环中,你就会得到答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文