我有一个核心数据库,我正在尝试使用块谓词创建一个获取请求,但是我收到一个未知谓词错误:
注意: employeeToHouse是我在为子类化NSManagedObject时创建的House类型的属性
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Place"];
request.predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
Place *sortPlace = (Place *)evaluatedObject;
CLLocation *placeLocation = [[CLLocation alloc] initWithLatitude:sortPlace.latitude.doubleValue
longitude:sortPlace.longitude.doubleValue];
CLLocationDistance distance = [placeLocation distanceFromLocation:self.userLocation];
sortPlace.distanceToUser = [NSNumber numberWithDouble:distance];
if(distance<3000)
{
return YES;
}
return NO;
}];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@ "distanceToUser"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
[self.loadingView removeSpinner];
[self setupFetchedResultsControllerWithFetchRequest:request];
然后我收到此错误:
NSInvalidArgumentException',原因:'未知的谓词类型 谓词:BLOCKPREDICATE(0x70ad750)'
我做错了吗?
答案 0 :(得分:13)
AFAIK你不能在CoreData中使用块谓词。
请在此处查看最高投票回答:NSFetchRequest and predicateWithBlock
原因是CoreData无法将块中包含的C代码转换为SQL查询。