Iphone 核心数据与多个关系问题
我将我的核心数据模型更改为多对多关系。我相信这是正确的模式。我需要能够做到以下几点: (1) 一顿饭可以包含许多食物 (2) 一个食物项目可以与许多膳食联系在一起 当我试图为某顿饭取出所有食物时,我遇到以下错误。这在一对多关系中起作用,但自从我把它改为一对多关系后就没有了Iphone 核心数据与多个关系问题,iphone,objective-c,ios,core-data,Iphone,Objective C,Ios,Core Data,我将我的核心数据模型更改为多对多关系。我相信这是正确的模式。我需要能够做到以下几点: (1) 一顿饭可以包含许多食物 (2) 一个食物项目可以与许多膳食联系在一起 当我试图为某顿饭取出所有食物时,我遇到以下错误。这在一对多关系中起作用,但自从我把它改为一对多关系后就没有了 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed her
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
- (NSFetchedResultsController *)fetchedResultsController
{
self.context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Food" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSPredicate *foodPredicate = [NSPredicate predicateWithFormat:@"meals == %@", self.meal];
[fetchRequest setPredicate:foodPredicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
只是谓词需要更改,我认为:
NSPredicate *foodPredicate = [NSPredicate predicateWithFormat:@"ANY meals == %@", self.meal];
既然
餐食
现在已经是一套了。这可能不是你的专长,你介意检查一下吗?如果膳食
与“食物”的关系与成反比,则不起作用。知道如何断言吗?@IEEngineer这篇老文章,但只是为了防止其他人有这个问题:我只能使用[NSArray arrayWithObject:obj]
中的任何objs==obj
来实现这一点。