联系人实体与用户具有多对一关系,该关系具有一个userID字段
联系人具有状态字符串属性,可以“批准”,“拒绝”,“待定”等
最喜欢的布尔属性
+ (NSSet *)fetchContactsWithUserID:(NSString *)userID approvedOnly:(BOOL)approvedOnly favoriateOnly:(BOOL)favoriateOnly {
NSString *predString = [NSString stringWithFormat:@"(user.userID = %@)", userID];
if (approvedOnly) {
predString = [predString stringByAppendingString:@" AND (status = approved)"];
}
if (favoriateOnly) {
predString = [predString stringByAppendingString:@" AND (favorite = YES)"];
}
NSPredicate *pred = [NSPredicate predicateWithFormat:predString];
return [self private_fetchEntitiesWithName:@"Contact" predicate:pred];
}
+ (NSSet *)private_fetchEntitiesWithName:(NSString *)name predicate:(NSPredicate *)predicate {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:name inManagedObjectContext:[self private_context]];
[fetchRequest setEntity:entity];
if (predicate) {
[fetchRequest setPredicate:predicate];
}
NSError *error;
NSArray *fetchedObjects = [[self private_context] executeFetchRequest:fetchRequest error:&error]; //CRASH ON THIS LINE!!!!!!
return [NSSet setWithArray:fetchedObjects];
}
堆栈跟踪:
没有错误注销(我已经启用nszombie)
最佳答案
@"(status = approved)"
不是有效的谓词。如果批准使用字符串,则谓词字符串应为@" AND (status = \"approved\")"
或@" AND (status = 'approved')"
。
如果直接在谓词中使用字符串文字,则必须将其用引号引起来。
NSFetchRequest将未用引号引起来的字符串视为键。
这样做是因为您实际上可以使用比较这两个谓词的谓词来检查两个属性是否相等,例如@"attribute1 = attribute2"
。
如果您使用包含%@
的谓词格式,如果要用作参数的对象是NSString,则NSPredicate将自动添加引号。
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"foo = %@", @"Bar"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"foo = Bar"];
NSLog(@"%@", p1);
NSLog(@"%@", p2);
产量:
xxx[4673:70b] foo == "Bar"
xxx[4673:70b] foo == Bar
关于ios - 核心数据获取请求崩溃而没有错误日志,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21802670/