Sqlite transaction blocks Android ui

In my Android application, AsyncTask has been running for quite some time. I start updating the database when the application starts. I need to wrap this in a transaction to drop back if the user exits the application before the task is completed. However, since wrapping the code in a transaction blocks ui until the task is completed. Why is this happening because the code is already running in a separate thread?

I use ORMLite, and this transaction shell is basically, the code for updating db goes inside call () .., before adding the code for updating db inside the transaction there was no ui lock ...

public ConnectionSource source; @Override protected Boolean doInBackground(Context... params) { try { TransactionManager.callInTransaction(source, new Callable<Void>() { public Void call() throws Exception { return null; } }); 
+4
source share
2 answers

Unfortunately, SQLite transactions are exceptional and block all other database activity. I suspect that even if you are in another thread, the user interface thread is doing some kind of database activity, which should wait for the transaction to complete.

+7
source

Since Android API 11 allows you to write a stream of letters and many read streams in WAL mode. You need to switch to WAL mode and use beginTransactionNonExclusive () to avoid blocking read streams.

 int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if(walModeEnabled) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { flags = flags | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } } SQLiteDatabase db = SQLiteDatabase.openDatabase(databasePath.getPath(), null, flags); // backward compatibility hack to support WAL on pre-jelly-bean devices if(walModeEnabled) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { db.enableWriteAheadLogging(); } else { Log.w(TAG, "WAL is not supported on API levels below 11."); } } 

Check out my article for more information on WAL mode and SQLite compatibility:

http://www.skoumal.net/en/parallel-read-and-write-in-sqlite/

0
source

All Articles