gpt4 book ai didi

iphone - 核心数据对多关系问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:28 26 4
gpt4 key购买 nike

我将核心数据模型更改为一对多关系。我相信这是正确的模型。我需要能够执行以下操作:

(1) 一顿饭可以包含很多食物(2) 一个Food iteme可以链接到多个Meals

enter image description here

当我尝试获取某餐的所有食物时出现以下错误。这在一对多关系时有效,但自从我将其更改为一对多关系后就不行了。

*** 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];

因为 meals 现在是一套。

关于iphone - 核心数据对多关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9740503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com