【发布时间】:2013-02-26 05:30:53
【问题描述】:
我正想弄清楚为什么这不起作用。
我有两个实体:
Quote
Customer
Quote 与 Customer 具有一对一的关系属性,简称为“customer”。客户有一个 CoreData objectID(显然)。我正在尝试搜索所有报价,并根据CustomerobjectID 返回与特定客户相关联的报价。通读所有教程,我设法得到了这个,但我一直在崩溃:
+ (void)fetchQuotesForCustomerID:(NSManagedObjectID*)objectID results:(void(^)(NSError* error, NSArray *fetchedResults))completion {
NSManagedObjectContext *context = [[QuoteGenerator sharedGenerator] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quote"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customer.objectID == %@", objectID];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"fetch error = %@", [error localizedDescription]);
completion(error, nil);
} else {
NSLog(@"fetch count = %d", fetchedObjects.count);
completion(nil, fetchedObjects);
}
}
输出错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath customer.objectID not found in entity <NSSQLEntity Quote id=13>'
谓词设置是否错误?通读文档是说您可以使用点语法来访问谓词中的属性。
请帮忙...
【问题讨论】:
-
如果一个客户可以有多个报价,为什么不使用一对多的关系并简单地使用核心数据来查找相关的报价对象?
-
@Gary 谢谢你的帮助。引导我找到正确的答案!
标签: objective-c core-data nspredicate nsmanagedobject nsfetchrequest