NSPredicate 通过关系而不是属性来过滤数据

发布于 2024-09-13 07:11:18 字数 1694 浏览 7 评论 0原文

这是我的问题。我有这个模型,一个[事件]有多个[日],并且[事件]中有一个称为天的关系,并且[日]中有一个反向关系事件。

现在我想传递 Event 对象并使用 NSFetchedResultsController 获取所有日期并将所有日期输入表中。这是我的代码:

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];

//set predicate  ->  ??
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"event = %@", self.currentEvent];
[fetchRequest setPredicate:predicate];

//set entity
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                            managedObjectContext:managedObjectContext 
                                                                                              sectionNameKeyPath:nil
                                                                                                       cacheName:nil];

不知何故, NSFetchedResultsController 实例将始终为不同的事件返回相同的内容。 我应该如何修复 NSPredicate?

Here is my question. I have this model one [Event] has multiple [Day] and there is a relationship called days in [Event] and there is a reverse relationship event in [Day].

Now I want pass the Event object and get all days using NSFetchedResultsController and feed all days in the table. Here is my code:

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];

//set predicate  ->  ??
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"event = %@", self.currentEvent];
[fetchRequest setPredicate:predicate];

//set entity
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                            managedObjectContext:managedObjectContext 
                                                                                              sectionNameKeyPath:nil
                                                                                                       cacheName:nil];

Somehow, the NSFetchedResultsController instance will always return the same thing for different event.
How should I fix the NSPredicate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

想你只要分分秒秒 2024-09-20 07:11:18

我想通了:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.currentEvent.days];

I figured it out:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.currentEvent.days];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文