0

I am trying to insert into my database from 2 AsyncTasks.

I have only one static instance of my SQLiteDatabase object, both AsyncTasks are executed with a SerialExecutor, when I am starting both DB transactions I am passing in a SQLiteTransactionListener and the onCommit callback is called for both inserts (I have tried using beginTransactionWithListenerNonExclusive() and beginTransactionWithListener()), and still the second insert fails to write into my db (no exceptions nothing indicating that it has failed).

Basically, I have an AsyncTaskLoader and in its onLoadFinished, I read my JSON and after that I fire the first AsyncTask where I start inserting into my DB, as this goes along there is a button that fetches me a list and populates a ListView of data from the same table where the first asyncTask writes into.

When I click on any of the items from the ListView, I fetch the data relevant for this selected item and insert a new entry in the same table (all of that fetching and inserting happens in a second AsyncTask).

The onCommit callback for the second asyncTask insert is called and when the second asyncTask finishes (onPostExecuted is called), I start a new activity where I am supposed to read the newly inserted data (interesting enough, that failing to insert happens randomly).

Even though both of my AsyncTasks are executed in serial (meaning the second one should wait for the first one to finish, right?) and the Listener tells me that the data is inserted, only the first AsyncTask successfully inserts the data. Why is this occurring?

admdrew
  • 3,790
  • 4
  • 27
  • 39
  • because only the first was executed check this discussion : https://groups.google.com/forum/#!topic/android-developers/8M0RTFfO7-M – TooCool Oct 13 '14 at 16:49
  • I am using executeOnExecutor(AsyncTask.SerialExecutor) for both with the idea that they won't insert at the same time in the DB. From what I understand if u try to insert into the db from 2 threads simontaneously it won't work ? – user3033112 Oct 13 '14 at 18:35
  • Possibly relevant: http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android/3689883#3689883 – Morrison Chang Oct 13 '14 at 19:06

0 Answers0