Can i use custom sqlite function in master data?

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?

+4
source share
3 answers

You are right, this is not possible using Core Data. Do not forget that other types of storage are possible. How to execute this request with XML?

If you just need to sort the results, why not set the sort descriptor in the array after performing the selection (assuming the data set is not too large)?

+1
source

I am going to suggest taking a different path.

It seems that from your user function you are doing some kind of work with GIS. I just did a quick google and saw that there was a GIS extension for sqlite: Gaia-SINS

I think you better roll up your own sqlite library and add an extension like this using other peoples GIS calculations. Why roll back your own GIS equations when other people have done it for you?

As a starting point, see other SO questions, for example: compiling-custom-sqlite-for-an-iphone-app

0
source

Cannot invoke a transient property in a fetch request. I have been working on the same issue many days after finding a workaround. I calculate the distance of each element and select n subjects of first interest. the following method takes the result of a multiple search in NSFetchedResultsController and returns the number p_NumberArroundMe.

 -(void) filteredCustomerPortfolioArroundMe:(NSInteger)p_NumberArroundMe for:(NSFetchedResultsController*)p_FetchedResultsController { NSFetchRequest *allEtabfetchRequest = [[NSManagedObjectModel CRMManagedObjectModel] fetchRequestFromTemplateWithName:@"AllEtablissements" substitutionVariables:nil]; NSArray *allEtabInDatabase = [[NSManagedObjectContext sharedManagedObjectContext] executeFetchRequest:allEtabfetchRequest error:nil]; NSArray *sortedList = [allEtabInDatabase sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"distanceFromUser" ascending:YES]]]; if ([sortedList count] > p_NumberArroundMe) { NSRange aRange; aRange.location = 0; aRange.length = p_NumberArroundMe; sortedList = [sortedList subarrayWithRange:aRange]; } NSMutableArray* keyList = [NSMutableArray array]; for (SPIREtablissementMin* item in sortedList) { [keyList addObject:item.partner]; } NSPredicate *predicateArroundMe = [NSPredicate predicateWithFormat:@"partner IN %@",keyList]; [p_FetchedResultsController.fetchRequest setPredicate:predicateArroundMe]; [self performFetch:p_FetchedResultsController]; } 
0
source

Source: https://habr.com/ru/post/1400008/


All Articles