开发者

Sorting geospacial items using coredata on iOS. Fastest way to update all 'distance' attributes

I'm developing an app that allows you to search for items and sort by distance from a given point. The approach I'm using is that when I ingest the data from the remote API, I calculate the distance from my current location and store that as an attribute of the Cor开发者_JAVA百科eData managed object. This works perfectly well when I'm sitting there developing at home, but when I use on the device, then move the device to a different location, the attributes are then incorrect since the distance has now changed for all elements.

The approach I'm considering is to run through the entire core data store and update the distance attribute when the iPhone changes location.

The obvious problem is that I can't do this for every single movement since I'd be running through the full CoreData store every time the user moves an inch. This would kill the battery and cause a slow user experience

Some solutions:

  1. I could limit the number of items I store in core data. There shouldn't be more than 100 or so relevant items in general.
  2. I could limit the number of times the recalculation happens by only recalculating when the user's location changes by some significant distance.
  3. I could do all of the recalculations in the background.

While these solutions should probably work, I imagine that there must be a more elegant solution.

Has anyone solved a similar issue to this?


As you probably know, you can configure CLLocationManager using distanceFilter and desiredAccuracy. This covers item 2.

100 items will not stress core data at all, unless they are gigantic items.

And you should always try to do non-UI things in the background.

So, I think you've got it covered; there's nothing inelegant about this approach.

Try it and see what, if any, performance problems you get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜