Dav*_*iss 5 iphone cocoa-touch core-data
我有一个具有许多Item实体的Group实体.Item实体可以位于多个组中.我模型的相关部分如下所示:

问题
我打电话的时候
[fetchedResultsController performFetch:&error]
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到此错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' ***
Run Code Online (Sandbox Code Playgroud)
我很确定问题出在我如何在这里设置谓词:
[NSPredicate predicateWithFormat:@"groups == %@", self.group];
Run Code Online (Sandbox Code Playgroud)
但我不知道我做错了什么.我已经阅读了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];
Run Code Online (Sandbox Code Playgroud)
这些都不起作用.这必须简单,但我无法弄清楚.我只是希望谓词过滤所有项目,以便只返回组成员(通过模型关系)的项目.你怎么做到这一点?
我有一个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;
Run Code Online (Sandbox Code Playgroud)
没有代码可以玩它很难100%,但你应该能够:
[NSPredicate predicateWithFormat:@"%@ in groups", [self group]];
Run Code Online (Sandbox Code Playgroud)
这假设您的fetch正在尝试撤回Item实体.
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |