Sort NSFetchedResultsController results by user location

I have an application that contains some locations in Core Data, and I want to show them to the user in order of proximity to the user's location. I am using NSFetchedResultsController to serve locations in a table view. I was thinking of creating a virtual access method that returns the distance from the user, which will be calculated using the "global" CoreLocationManager, but the failure occurs with an error:

Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "distance to the key point" FromCurrentLocation was not found in the entity <NSSQLEntity Location id = 4> '

I also give the user the ability to sort in alphabetical order, so I would prefer it if I kept the NSFetchedResultsController if possible.

How can I do it?

Thanks!

+6
objective-c iphone core-data nsfetchedresultscontroller
source share
3 answers

The problem (or missing feature) of NSFetchedResultsController is the missing ability to use transient attributes for sort order and section path.

I also struggled with this, so you might be wondering:

NSFetchedResultsController localized sections sorted

I'm sure you could use this approach if you could guarantee a limited set of records. But I would really like to see a more complex solution.

+3
source share

Virtual accessors will not work in Core Data unless you have the transient attribute defined in the object. NSFetchedResultsController (and all other operations with master data) look at the graph of objects before they attempt to perform any operation. If the virtual property is not displayed in the object definition, then NSFetchedResultsController will report that such an attribute does not exist.

You want to define a transition attribute in an entity called distanceFromCurrentLocation , and you should be able to write virtual accessors and see NSFetchedResultsController.

+1
source share

I tried to do the same to sort the results by distance from the location. Here is one of my questions:

Key data and kernel location

It turned out that you need to load all the places in memory to compare them. I ended up using SQLite, which is harder to implement, but has caused far fewer headaches for this kind of function.

This may not be the answer you need, but if you have a large dataset, I would recommend SQLite. As a result, it became easier and easier for me to cope.

+1
source share

All Articles