R – how to use coredata to fetch a list of places ordered by distance

core-dataiphone

I have a coredata based iphone app and have an Entity that has latitude and longtitude as properties. How can I return all my entities ordered by distance from the current location? I previosuly did this without coredata writing a sql query, but am new to coredata and can't figure it out. I would like to use the NSFetchedResultsController to pass straight to my tableview.

Any help please?

Best Answer

I have a similar situation - and although the above is true with regards to not being able to use the NSFetchedResultsController you can still use core data to retrieve your data.

I also used sqlite3 before - and my fetch was done as a series of fetches of increasing bounding box sizes - until a size is reached where I have 'enough' results. In my case I double the latitude and longitude span for every iteration - giving approx. a quadrupling of the area for each iteration.

This method works fine in Core Data too - but as it is also stated above you need to copy the objects into a new array and sort them using your own method after the fetch is done.

Related Topic