I have AsyncTask that processes some background HTTP stuff. AsyncTask runs on schedule (Alarms/service) and sometime user executes it manually.
I process records from SQLite and I noticed double-posts on server which tells me that sometime scheduled task runs and at the same time user runs it manually causing same record to be read and processed from DB twice. I remove records after they processed but still get this.
How should I handle it ? Maybe organize some kind of queing?
You can execute your AsyncTask’s on an
ExecutorusingexecuteOnExecutor()To make sure that the threads are running in a serial fashion please use:
SERIAL_EXECUTOR.Misc: How to use an Executor
If several activities are accessing your DB why don’t create a sort of gateway database helper and use the
synchronizedblock to ensure only one thread has access to it at an instant