I have a list of geographical locations in Core Data (the name of the object is "Stops").
I want to sort them by current location, so I can show the user which locations are nearby. I am using NSFetchedResultsController so that the results can be easily displayed in a UITableView.
I am using the following code to try this view:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"stop_lat" ascending:YES comparator:^NSComparisonResult(Stops *obj1, Stops *obj2) { CLLocation *currentLocation = locationManager.location; CLLocation *obj1Location = [[CLLocation alloc]initWithLatitude:[obj1.stop_lat doubleValue] longitude:[obj1.stop_lon doubleValue]]; CLLocation *obj2Location = [[CLLocation alloc]initWithLatitude:[obj2.stop_lat doubleValue] longitude:[obj2.stop_lon doubleValue]]; CLLocationDistance obj1Distance = [obj1Location distanceFromLocation:currentLocation]; CLLocationDistance obj2Distance = [obj2Location distanceFromLocation:currentLocation]; NSLog(@"Comparing %@ to %@.\n Obj1 Distance: %f\n Obj2 Distance: %f",obj1.stop_name, obj2.stop_name, obj1Distance, obj2Distance); if (obj1Distance > obj2Distance) { return (NSComparisonResult)NSOrderedDescending; } if (obj1Distance < obj2Distance) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [fetchRequest setEntity:[NSEntityDescription entityForName:[Stops entityName] inManagedObjectContext:context]]; frcNearby = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; NSError *error; BOOL success = [frcNearby performFetch:&error]; if (error) NSLog(@"ERROR: %@ %@", error, [error userInfo]);
However, my NSFetchedResultsController simply returns all my items sorted by the key I specified ("stop_lat"), and not sorted by the user's current location.
It seems like my comparator block is never called because NSLog never prints there.
What am I missing here?
Aaron brager
source share