I’m trying to do a custom calendar in my app with event sync with a WebService.
Everything goes just fine (well, not really).
The Calendar Activity starts an AsyncTask in order to get current month events and store them in the database and the onPostExecute method updates the view to properly mark each day.
My problem comes when the user gets to the calendar activity and then quickly goes back to main activity several times, let’s say for instance
Main Activity > Calendar Activity (Press Back button) > Main Activity > Calendar Activity (Press Back button) > …
At this point Activity freezes due to a database lock has not been available for 30 sec. or App crashes due a SQLiteConstraintException: error code 19
EDIT:
SQLiteConstraintExceptionbehaviour no longer happens
Because being updated is very important in my app, every time I re-enter the Calendar Activity I delete the database and re-populate it with server new data even tough it is the same, and at this point is where the lock is made.
EDIT:
I’ve changed
Delete > Insertstyle to:
Updateall data (setflagcolumn = 0) >Insertnew data (setflagcolumn = 1) >Delete(whereflagcolumn = 0)
I have already tried this solution to cancel the AsyncTask on Activity finish with no success
I am sure the problem is I don’t completely understand the process of what I am doing so I am missing something really important (or basic).
Any help improving my code or just pointing me to the right direction, would be much appreciated.
Thanks in advance!
EDIT:
My application has an
Aplicationwhich is in charge of creating a single Database connection (getWriteableDatabase();) and this unique conection is reachable troughMyApp.getDatabase();method.
That will likely create & start a new
AsyncTaskeach time you you open the calendar.Each
AsyncTaskwill still run if you don’t cancel it expicitly.And it depends on the Android version / executor you use to execute the
AsyncTaskif they run in parallel or serialized. The default behavior for >= Honeycomb would be serial execution. Explained in the Order of execution section inAsyncTaskdocumentation.If you use serial AsyncTask you can have several of your (old) tasks queued up and it can take quite some time before your most recent task is executed. It might be a good idea to cancel tasks when you leave an Activity.
The
database lock has not been available for 30 sec.warning you get indicates that you write in one thread and try to access the database from another thread at the same time. Not sure if that requires two write threads in parallel.The problem is not using different
SQLiteDatabaseinstances. I think you would get errors instead of that warning if you were doing that since Android can’t check if some random SQLiteDatabase object has a write lock on the database.So what can you do:
I assume you use transactions since locking the database for long without transaction is pretty much impossible.
db.yieldIfContendedSafely(). That will check if another thread wants to access the database and temporarily ends the transaction. Drawback: It will commit the change and you can’t rollback the whole transaction.SQLiteDatabase#enableWriteAheadLogging()&beginTransactionNonExclusive()as mentioned in the write-ahead-logging documentation. It reads like it could allow multithreaded access without locking problems. I have not investigated further if that works and what side-effects it has.