1

I'm trying to make my own gps-tracker, mainly for bike-rides. I managed to make a usable app, at least for personal use, but I wanted to improve it, and have added ContentProviders, Fragments, CursorAdapters and a Service to receive the onLocationChanges from GPS.

However, I have really no idea how to handle the stream of Locations I'm receiving. My old app just saved them to an ArrayList, so right now my Service is sending a Broadcast to my Activity, that saves them to the ArrayList.

Problem is, that when the ride is over, it takes from 5-15 seconds to save the locations to sqlite (yes, I'm using compiledstatement and transaction), and I would like to avoid that, by saving the locations when received. When I tried to do that, my app became unresponsive (as expected), as I was doing the insert in the UI thread, and I do receive location updates often.

This is of course the most important aspect of the app, so what is the solution?

  • I could do the insert in a thread, but since inserting a single record is expensive, I'm not sure it could keep up.

  • I could write 25 records (or more) at a time in a transaction, but there will be some logic to keep track of what is saved and what is not.

Is there other options for a better user-experience ?

2 Answers 2

3

Use an IntentService to delegate the saving to another thread, then use applyBatch to do inserts.

Sign up to request clarification or add additional context in comments.

1 Comment

But that would still make the data unavailable for those 5-15 seconds (or more on a longer trip), which is what I'm trying to avoid.
0

Personaly I think the gps track would be better in a file than embeded in the database tracks are lickly to be just big. It's the same senario as pictures where embeding in the database is not the recomended solution. The databases would just hold some summary information and a reference to the file. You wouild write out the file as you go so no big pauses at the end. You keep the local ArrayList as well while you are recording if you are using it for displaing a path on a map or a graphical plot etc. That's the way I am doing things in my biking app IpBike.

2 Comments

Thanks. That might be something to consider, and I would assume it would be faster to write to a file, instead of database. Are you keeping the file open during recording ?
Yes I write the file in a service and just keep it open. I do intermitently flush it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.