Compute columns with master data such as SQL

I sort the stores to get the closest stores. In addition, I want to know how far this store is. The SQL query will look like this:

select *, 6371 * 2 * ATAN2(SQRT(POWER(SIN((RADIANS(?) - RADIANS(latitude))/2),2) + COS(RADIANS(latitude)) * COS(RADIANS(?)) * POWER(SIN((RADIANS(?) - RADIANS(longitude))/2),2)), SQRT(1-POWER(SIN((RADIANS(?) - RADIANS(latitude))/2),2) + COS(RADIANS(latitude)) * COS(RADIANS(?)) * POWER(SIN((RADIANS(?) - RADIANS(longitude))/2),2))) AS DISTANCE FROM stores ORDER BY DISTANCE 

these question marks will be replaced by the longitude and breadth of users. (latitude, latitude, longitude, latitude, latitude, longitude)

How can I achieve this using the iPhone SDK using Core Data?

+4
source share
2 answers

CoreData alone cannot do this, because distanceFromCurrentLocation here will be a transient property (and you cannot get transient properties). But you can still do it the natural way by selecting objects in NSMutableArray and then sorting this array based on the distanceFromCurrentLocation transient property.

Depending on how often you do this and how often the user moves far enough to require recounting, it may be more efficient from time to time to recount the constant -currentDistance property and then use simple predicates. This is a very effective solution if you are looking for a much more frequent movement of the user, because you can easily save one constant variable locationOfLastRecalculation and only recount once when the user moves enough. Since this will be retained during a restart, a general performance improvement over dynamic computing can be significant.

+3
source

From what I know about part of the main data (at least ORM), there is no way to perform aggregate functions or mathematical functions, for example, what you are trying to do. One has the ability to make predicates that equate to a WHERE clause, but not to select functions.

I believe that you can bypass Core Data and do it in sqlite.

Jack

+1
source

All Articles