質問

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 ?

役に立ちましたか?

解決

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()

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top