Doesn’t seem to be anything about this online, I need to run fetch query ordered by distance from the users current location. My first plan was to create a transient property…
- (CLLocationDistance)distance {
return [self.location distanceFromLocation:locationManager.location];
}
But then I found out core data won’t fetch based on a transient property, which I thought might be the case as the fetch is turned into a SELECT statement with an ORDER BY clause so my distance property needs to exist in the database.
Ok so my second idea was to achieve this the same way I did in another project that used SQLite without Core Date using a custom sqlite function created with sqlite3_create_function…
static void sqliteDistance(sqlite3_context *context, int argc, sqlite3_value **argv) {
if(sqlite3_value_type(argv[0])==SQLITE_NULL || sqlite3_value_type(argv[1])==SQLITE_NULL) {
sqlite3_result_null(context);
} else {
sqlite3_result_double(context, [locationManager.location distanceFromLocation
[[CLLocation alloc] initWithLatitude: sqlite3_value_double(argv[0]) longitude: sqlite3_value_double(argv[1])]]);
}
}
Now question is how would I call this function from a core data fetch query? Im guessing its probably not possible as core data’s purpose is to abstract away the details of the backing store but maybe someone knows different?
You are right, this isn’t possible using Core Data. Don’t forget that other backing store types are possible. How would it execute that query against XML?
If you just need to sort the results why not set a sort descriptor on the array after you perform the fetch (assuming the dataset isn’t too large)?