Android threading and database locking

后端 未结 7 1752
夕颜
夕颜 2020-12-07 10:31

We are using AsyncTasks to access database tables and cursors.

Unfortunately we are seeing occasional exceptions regarding the database being locked.

7条回答
  •  青春惊慌失措
    2020-12-07 11:14

    I solved this same exception just by making sure all my database opens have closes, and (more importantly) to assure this, making the scope of each database instance local ONLY to the method that needs it. ContentProvider is a good, safe class to use when accessing a db from multiple threads, but also make sure you're using good db practices:

    • Keep db instances local (no SQLiteDatabase class members!)
    • call close() on the db in the same method in which it's opened
    • call close() on the cursors you get from the db
    • listen to LogCat for any complaints that SQLiteDatabse might have

提交回复
热议问题