I am developing an application that allows you to search for items and sort by distance from a given point. The approach I use is that when I view data from a remote API, I calculate the distance from my current location and save it as an attribute of the CoreData managed entity. This works fine when I sit there, developing at home, but when I use this device, move the device to another place, the attributes are then incorrect, since now the distance has changed for all elements.
The approach I'm considering is to run the entire master data store and update the distance attribute when the iPhone changes location.
The obvious problem is that I cannot do this for every single movement, since I will work through the full CoreData repository every time the user moves an inch. This will kill the battery and cause the user to slow down.
Some solutions:
- I can limit the number of items stored in the master data. In general, there should not be more than 100 or so relevant elements.
- I could limit the number of times when recalculation occurs, only recounting when the user's location changes to some significant distance.
- I could do all the recounts in the background.
Although these solutions should probably work, I believe there should be a more elegant solution.
Has anyone resolved a similar problem?
source share