1

My app makes several calls to getFromLocationName from the android.location.Geocoder; class, this is currently handled in my activity which also displays some data depedant on the results of that call.

This can be quite slow, for example before the activity can load it first has to complete the various calls, and that is reliant on a network connection.

Once I have the results, I don't really need to refresh that (I mean how often do locations and geo coordinates change?)

My question : How can I best avoid having to query for this data each time my app runs. Should I do something with a service that loads them all on startup? (could be quite slow if it has to query 50+ times). OR should I make the call, store results in SQL, then only query next time if I don't currently hold the results?

Jimmy
  • 16,123
  • 39
  • 133
  • 213

1 Answers1

2

If you want to quickly cache it, I would recommend storing the most recent results in a SQLite database. You could probably even get away with using the PreferenceManager for quick storage if you don't need the database for anything else. Just store the latest co-ordinates and the datetime, then check to see if the datetime has expired after X minutes or X hours (if that's what you're wanting to do).

Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
  • Well I wouldn't need SQL lite for anything else (at this stage). Would there be any performance trade off to consider ie is it faster than manually querying the location service each time? – Jimmy Nov 05 '10 at 22:28
  • 1
    @James I would say that the most important factors is that 1) it is locally stored on your phone and quick to access 2) getting your gps co-ordinates will take a while for the gps to lock in (or via cell towers if gps is off). So speed wise it'll be fast, but the biggest problem is how "cold" or stale (how recent) your last GPS co-ordinates were. And you'll still have to find the co-ordinates the very first time. And users may move a lot (if by say, a car). I still think cacheing might help you (perhaps give the user a manual GPS refresh option?) – Bryan Denny Nov 05 '10 at 22:34
  • Sounds good, what about the initial query and storage/reading from SQL Lite, should that all reside in a service, or is it safe to remain in activity? (Just plugging for advice here) – Jimmy Nov 05 '10 at 22:37
  • You should be fine calling it from the activity. See this for a basic sql lite database tutorial: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ – Bryan Denny Nov 05 '10 at 22:44