Reputation: 8608
I'm using an IntentService to sync my SQLite database (managed by a Content Provider) with a webservice. My problem is that the service is triggered when the user refreshes the screen, hence there can be more of one service running at the same time. I'm afraid that might cause concurrency issues: data corruption for example.
My question is: Is there an "Android" way to prevent this to happen? Or do I have to build a queueing system myself? I know I could do put my whole service code in an atomic block, but I'm looking for something more elegant.
Thanks!
Upvotes: 0
Views: 57
Reputation: 28063
IntentService
already handle for you a queueing system. From the documentation:
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
IMHO if you want a more robust and elegant solution to sync your ContentProvider
with a webservice you should create a SyncAdapter which is much more powerful (it can even monitor your ContentProvider
and trigger the webservice call).
Upvotes: 4
Reputation: 15775
If the service is being triggered by Intents, then it will follow the normal lifecycle and the callbacks will get hit on the main UI thread of your app's process. The service is not re-spawned with multiple instances. If you spawn your own background thread or AsyncTask in the service to do some heavy lifting for you (i.e. process your data) then you'll need to protect things accordingly.
Upvotes: 1