2
votes

I want an NSPredicate like so:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(latitude - %@) < %@",coordsLatitude, kMaxLatLong];

I basically want to find GPS coords in Core Data that are within a certain distance from userLocation.

  • latitude is the latitude attribute of each gps entity.

  • coordsLatitude is the latitude of userLocation.

  • kMaxLatLong is set to 1

I'm getting a EXC_BAD_ACCESS with this line. I'm thinking it's due to my predicate just being badly formed, in particular the subtraction operator. However, I could not find anything stating how to use subtraction with NSPredicates, nor with NSExpressions.

1

1 Answers

3
votes

I'd do this the other way around.

Work out the minimum and maximum latitude and then use the predicate to find everything between.

i.e.

float coordsLatitude = //whatever

float minLatitude = coordsLatitude - kMaxLatLong;
float maxLatitude = coordsLatitude + kMaxLatLong;

NSPredicate *minPredicate = [NSPredicate predicateWithFormat:@"latitude >= %f", minLatitude];
NSPredicate *maxPredicate = [NSPredicate predicateWithFormat:@"latitude <= %f", maxLatitude];

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[minPredicate, maxPredicate]];

Then use the compoundPredicate in your fetch request.