Sorting CoreData fetch results based on computed [meta]attribute

Discussion in 'iOS Programming' started by RonC, Jun 6, 2011.

  1. RonC, Jun 6, 2011
    Last edited: Jun 6, 2011

    macrumors regular

    Oct 18, 2007
    (More CoreData-related stuff from Ron C)

    I have a table in which I store, among other data, a location (as in lat/long). Consider this as a simplified version of that table:

    Entity: City
    • cityName: String
    • location: Transformable (CLLocation)

    I generated the class and now I want to sort these cities by their distance from me. My location is stored as a CLLocation, and I know how to compute the distance between CLLocations (loc1 distanceFromLocation: loc2). What I'd like to do is use this computed value for sorting the results in the fetched results controller, but I don't have the foggiest idea of how to write the predicates to make this happen.

    Any thoughts?

    UPDATE: Apparently this isn't really possible WITHOUT a) storing distance somehow (in an associated array or an attribute) OR trolling through the data and updating the stored distance. I'm not opposed to it, as the number of records is likely to be small (<100), but it leaves me feeling a little "icky" like there should be a better way...

Share This Page