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

标签 iphone core-data nspredicate relationship

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

现在我想传递 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?

最佳答案

我明白了:

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

关于iphone - NSPredicate 通过关系而不是属性来过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3420874/

相关文章:

ios - 如何在 swift 中使用 MR 从 coreData 获取数据

core-data - NSPredicate 用于 NSManagedObject 的字符串属性的长度

iphone - 在 OpenGL ES iPhone 中加载非二次方图像

iPhone 图片在后台上传

iphone - 支持的界面方向和 shouldAutorotateToInterfaceOrientation

swift - 如何将 NSFetchedResultsController 的部分 sectionNameKeyPath 设置为属性的第一个字母,而不仅仅是 Swift 中的属性,而不是 ObjC

ios - NSDecimalNumber 总和

objective-c - 循环遍历特定文本的文本字段中的值

swift - 如何使用 NSPredicate 判断一个 Int 列表是否包含一个 Int 数字?

iphone - ARC 和 MBProgressHUD