2

我的谓词不断使我的应用程序崩溃,并显示消息 Unsupported function expression FUNCTION(SELF, "filterDistanceWithLatitude:" , latitude, longitude)。有谁知道如何解决这一问题?

- (void)setUpFetchedResultsController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"NextTime"]; //Retrieve data for the place entity
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]]; //How to sort it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil]; //Puts the data in a NSFetchedResultsController which is oddly located in CoreDataTableViewController //Puts the data in a NSFetchedResultsController which is oddly located in CoreDataTableViewController
    self.filtered = self.fetchedResultsController.fetchedObjects;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FUNCTION(self, 'filterByDistanceWithLatitude:', latitude, longtitude) > 20"];
    self.filtered = [self.filtered filteredArrayUsingPredicate:predicate];
}

- (double)filterByDistanceWithLatitude:(NSNumber *)latitude andLongitude:(NSNumber *)longitude
{
    CLLocationDegrees latitudeCoor = [latitude doubleValue]; //Puts the latitude into a NextTime object.
    CLLocationDegrees longitudeCoor = [longitude doubleValue]; //Puts the longtitude into a NextTime object.

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:latitudeCoor longitude:longitudeCoor];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.currentLocation.latitude longitude:self.currentLocation.longitude];

    NSNumber *distance = [[NSNumber alloc] initWithDouble:[loc1 distanceFromLocation:loc2]];

    return [distance doubleValue];
}
4

2 回答 2

2

(基于 SQL 的)Core Data 存储的获取请求不能使用基于 Objective-C 的谓词或排序描述符。您只能过滤存储在数据库中的属性。

以下是“核心数据编程指南”中的相关文档:

您不能使用基于瞬态属性的谓词进行获取(尽管您可以使用瞬态属性自己在内存中进行过滤)。...总而言之,如果您直接执行 fetch,则通常不应将基于 Objective-C 的谓词或排序描述符添加到 fetch 请求中。相反,您应该将这些应用于获取的结果。

获取和存储类型之间存在一些交互。...另一方面,SQL 存储将谓词和排序描述符编译为 SQL,并在数据库本身中评估结果。这样做主要是为了性能,但这意味着评估发生在非 Cocoa 环境中,因此依赖 Cocoa 的排序描述符(或谓词)无法工作。

于 2012-11-08T16:05:43.747 回答
0

'filterDistanceWithLatitude:' 可能应该是 'filterByDistanceWithLatitude:'

于 2012-11-08T16:04:32.780 回答