NSPredicate 与 FUNCTION 不工作 [英] NSPredicate With FUNCTION Not Working

查看:28
本文介绍了NSPredicate 与 FUNCTION 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My predicate keeps on crashing my app with the message Unsupported function expression FUNCTION(SELF, "filterDistanceWithLatitude:" , latitude, longitude). Does anyone know how to fix this?

- (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 的谓词或排序描述符.您只能过滤存储在数据库中的属性.

A fetch request for a (SQL based) Core Data store cannot use Objective-C based predicates or sort descriptors. You can only filter on attributes stored in the data base.

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

Here is the relevant documentation from the "Core Data Programming Guide":

您不能使用基于瞬态属性的谓词来获取(虽然你可以使用瞬态属性在内存中过滤你自己).... 总而言之,如果您直接执行 fetch,您应该通常不会将基于 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.

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

    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.

    这篇关于NSPredicate 与 FUNCTION 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆