Android SQLite tip 1 – Wrap batches of writes in a transaction

Posted on August 16, 2011


I’ve been working on the Shhmooze Android app. When the app is first run there are quite large amounts of data that need to be downloaded and persisted to an Android SQLLite db on the device(around 200o records). I was running into an issue where these writes to the db were taking a very long time. After running a trace using the  Debug class I discovered that the main issues were the compilation of the SQL statements and the individual writes to the db each row was taking about 15-30ms to write , this was not good.

I experimented and found a couple of small tweaks that make a huge difference, this first is wrapping the batch of writes in a transaction. The code looks like this:

		for (IRemoteUser user : users) {

As you can see this is pretty simple, start a transaction, write the objects to the db(in this case I’m doing this in a child method) and then commit the transaction.

Posted in: Android, SQLLite