objective-c - NSPredicate 与 FUNCTION 不起作用

标签 objective-c ios core-data nspredicate cllocation

我的谓词不断使我的应用程序崩溃,并显示消息不支持的函数表达式 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];
}

最佳答案

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

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

You cannot fetch using a predicate based on transient properties (although you can use transient properties to filter in memory yourself). ... To summarize, though, if you execute a fetch directly, you should typically not add Objective-C-based predicates or sort descriptors to the fetch request. Instead you should apply these to the results of the fetch.

There are some interactions between fetching and the type of store. ... The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work.

关于objective-c - NSPredicate 与 FUNCTION 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292582/

相关文章:

ios - 如何将 UIWebView 链接到网站

ios - 修改 UIObject 的基类与修改 CALayer 有什么区别?

iphone - 合并 CoreData 托管上下文时出现异常/崩溃

ios - 处理 Core Data 保存错误(Cocoa 错误 1570.)的方法

cocoa - 核心数据错误与异常第 3 部分

ios - 如何在 iOS 中不使用 UIApperance 更改一个类的外观?

ios - 更改主屏幕上显示 "APP NAME is using your location"的蓝色条的文本

iphone - 更改核心图中的 y 轴标签间隔

c# - MonoTouch - UIView.Animate 完成回调未在按钮 touchUpInside 委托(delegate)内调用

ios - 选择UITableView的行并不重要?