Recommended design pattern for writing to SQLite database in Android Recommended design pattern for writing to SQLite database in Android sqlite sqlite

Recommended design pattern for writing to SQLite database in Android


I can't really say much about the synchronization/deadlock part, that would be hugely dependent on the rest of your code. Since DataManager class doesn't really interact with the UI, you might want to use a service (IntentService) rather than an AsyncTask. You can show notifications when you are done syncing. You don't really need onPostExecute() if you are not calling UI code.


You may want to consider this info from the SDK (http://developer.android.com/reference/android/os/AsyncTask.html)

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.


FYI, every SQL statement ran on SQLite is ran under a transaction even if you don’t specify one.

Check below threads if you are doing Bulk Insert in SQLite:

  1. Android Database Transaction
  2. SQLite Bulk Insert