Question

I am working on making a downloader in Android. To allow the database to be shared across multiple activities I created a singleton instance of the WritableDatabase.

Each thread that downloads some content needs to write to the database how much data, in total, has been downloaded. Since multiple threads will be accessing this instance, do I need to use a synchronized code block while entering the values into the database ?

Was it helpful?

Solution

It is safe to use SQLiteDatabase from multiple threads, but by default each operation will block others. To enable concurrent updates, use enableWriteAheadLogging. Read more at http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()

OTHER TIPS

It's like the Readers/Writers problem, there's no problem when multiple threads are reading from the Database, but when they're trying to write, you must use a synchronized block to allow a thread-safe operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top