NSPredicate with FUNCTION function not working

My predicate continues to crash my application with the message Unsupported function 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]; } 
+2
source share
2 answers

A sample query for a Core Data warehouse (based on SQL) cannot use Objective-C predicates or sort descriptors. You can only filter attributes stored in the database.

Here is the relevant documentation from the Master Data Programming Guide:

You cannot get using a predicate based on transient properties (although you can use transient properties to filter in memory yourself) .... To summarize, however, if you make a selection directly, you should usually not add predicates based on Objective- C or sort the descriptors for the select query. Instead, you should apply them to the selection results.

There are some interactions between the selection and the type of storage .... SQL storage, on the other hand, compiles the predicate and sorts the descriptors for SQL and evaluates the result in the database itself. This is primarily done for performance, but this means that the evaluation takes place in a non-Cocoa environment and therefore sorts descriptors (or predicates) that rely on Cocoa cannot work.

+2
source

'filterDistanceWithLatitude:' probably should be 'filterByDistanceWithLatitude:'

0
source

All Articles