1

So I have a Core Data with an entity containing a longitude and latitude as NSNumbers. I want the NSFetchRequest's Predicate to filter based on how far the object in the database's location is from my current location. This would require me to convert the stored coordinates to CLLocationDegrees and then put them in a CLLocation object. Finally, I can do the compare with the CLLocation distanceFromLocation.

I worded this as best as I can, does anyone have any ideas how I can do this with an NSPredicate?

Thanks!

yeesterbunny
  • 1,847
  • 2
  • 13
  • 17
gsapienza
  • 351
  • 1
  • 3
  • 8

1 Answers1

2

Try block-based predicate:

CLLocationDistance maxDistance = 100.0;
CLLocation *currentLocation = ...;

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id location, NSDictionary *bindings) {
    return [location distanceFromLocation:currentLocation] <= maxDistance;
}];
Cfr
  • 5,092
  • 1
  • 33
  • 50
  • 5
    Important to note that predicateWithBlock with Core Data is only supported for in-memory and atomic stores and _not_ for SQLite-based stores which is most likely what the OP using. – David Rönnqvist Nov 08 '12 at 21:23