When should I do certain SQLite operations on another thread(not the main thread)?

前端 未结 3 423
情歌与酒
情歌与酒 2020-12-01 05:05

My Android application includes an SQLite database with an SQLiteOpenHelper class to help manage it. During application use, the user may perform some operatio

3条回答
  •  隐瞒了意图╮
    2020-12-01 05:47

    General rule for everything: If it's fast enough, do it on the main thread. If not, use a worker thread.

    Unless you have a ridiculously huge database, a single operation almost never warrants a separate thread. Databases in general are designed to scale well, but of course a very big database (10,000+ rows?) will be a bit slower than a small one. 30 rows, however, is nothing.

    I would start threading stuff if you have a lot of operations going on, like a bunch of queries, or complicated queries that span several tables.

    As with everything - profile your app, and if it's too slow, optimize. Don't write an awesome synchronized super-duper multi-core-ready database handler if none of your queries takes longer than 2ms.

提交回复
热议问题