There seems to be nothing about this, I need to run a fetch request sorted by distance from users' current location. My first plan was to create a transition property ...
- (CLLocationDistance)distance { return [self.location distanceFromLocation:locationManager.location]; }
But then I discovered that the main data would not be retrieved based on the transient property, which I thought might be the case, since the selection turns into a SELECT statement with an ORDER BY clause, so my distance property must exist in the database.
So, my second idea was to achieve this in the same way as in another project that used SQLite without Core Date, using a custom sqlite function created using 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 the question is, how can I call this function from a query to select the main data? I suppose it is probably not possible, since the main purpose of the data is to abstract data from the backup storage, but maybe someone knows different?
source share