【发布时间】:2010-08-05 20:05:05
【问题描述】:
我有一个包含许多 Item 实体的 Group 实体。一个项目实体可以在多个组中。我的模型的相关部分如下所示:
问题
当我打电话时
[fetchedResultsController performFetch:&error]
我在控制台中收到此错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' ***
我很确定问题在于我在这里如何设置谓词:
[NSPredicate predicateWithFormat:@"groups == %@", self.group];
但我不知道我做错了什么。我已经阅读了 NSPredicate 文档并尝试过:
[NSPredicate predicateWithFormat:@"ANY groups IN %@", [NSArray arrayWithObject:self.group]];
[NSPredicate predicateWithFormat:@"ANY groups LIKE %@", self.group];
[NSPredicate predicateWithFormat:@"ANY groups == %@", self.group];
[NSPredicate predicateWithFormat:@"ANY groups IN %@", [NSArray arrayWithObject:self.group]];
[NSPredicate predicateWithFormat:@"groups == %@", self.group];
这些都不起作用。这必须很简单,但我无法弄清楚。我只是希望谓词过滤所有项目,以便仅返回作为组成员(通过模型关系)的项目。你是怎么做到的?
我有一个 NSFetchedResultsController,我试图将其配置为仅显示特定组中的项目。我设置 NSFetchedResultsController 的代码如下所示:
// create the fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// configure the entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// configure the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"groups == %@", self.group];
[fetchRequest setPredicate:predicate];
// configure the sort descriptors
NSSortDescriptor *indexSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"uniqueID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:indexSortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// create the fetched results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"ItemsCache"];
// configure the fetched results controller
aFetchedResultsController.delegate = self;
【问题讨论】:
-
我曾经通过打破另一个实体的关系来处理多对多关系。可能是以下问题显示了我是如何做到的。 stackoverflow.com/questions/2822625/… 您还可以使用数据模型编辑器中的谓词编辑器来构造谓词。希望对你有帮助,
-
关于谓词编辑器的提醒很有帮助。我还没弄明白,谢谢!
标签: iphone cocoa-touch core-data