我应该在什么时候做某些SQLite的操作在另一个线程(没有主线程)? [英] When should I do certain SQLite operations on another thread(not the main thread)?

查看:186
本文介绍了我应该在什么时候做某些SQLite的操作在另一个线程(没有主线程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序包含一个SQLite数据库与 SQLiteOpenHelper 类来帮助管理它。期间应用的使用中,用户可以执行某些操作,如在数据库上添加/删除/更新等等。

My Android application includes an SQLite database with an SQLiteOpenHelper class to help manage it. During application use, the user may perform some operations such as adding/deleting/updating etc on the database.

在某些方面操作的规模将是已知的,像这样的:

At some points the size of the operation will be known, like this:

  1. 在用户点击按钮保存项目
  2. SQLiteDatabase 执行一个插入查询
  3. 在用户继续使用应用程序
  1. user clicks button to save item
  2. the SQLiteDatabase performs a single insert query
  3. user continues using app

在应用程序的其他区域,操作可能很大,如插入10+物品进入数据库的一次。

At other areas of the app, the operation may be large, like inserting 10+ items into the database all at once.

问题:

  • 我应该线程简单的操作,如插入/更新/删除/查看1个项目?
  • 将它需要更长的时间来插入第1项到其中包含很多项目(如30 +)的表比它会采取插入到表中没有的项目?
  • 如果我不需要线程这样简单的操作,在你的建议是什么时候,我的开始线程呢?
  • should I thread simple operations like inserting/updating/deleting/viewing 1 item?
  • will it take longer to insert 1 item into a table which contains many items(like 30+) than it would take to insert into a table with no items?
  • if i don't need to thread such simple operations, at what point do you suggest i start threading them?

当我说线程我的意思是使用一个线程,是不是在主UI线程。

when i say thread i mean using a thread that is not the main UI thread.

编辑:我知道,小操作不会花太多的时间,我很可能逃脱做他们的主线程上。我只是担心,这将是不好的做法,在执行他们的主线程上,并想澄清!

edit: I realize that small operations do not take much time and i could very well get away with doing them on the main thread. I am just concerned that it would be bad practice to be executing them on the main thread and would like clarification!

推荐答案

一切一般规则:如果它足够快,这样做在主线程。如果没有,用一个工作线程。

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

除非你有一个可笑的庞大的数据库,一个单一的操作几乎没有值得一个单独的线程。一般数据库设计具有良好的伸缩性,但当然还有一个非常大的数据库(10,000行?)会比一个小的有点慢。 30行,但是,是什么都没有。

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.

如同一切 - 配置您的应用程序,如果它的速度太慢,优化。不要写一个真棒同步超级骗子多核就绪的数据库处理程序,如果没有你查询的时间超过2ms的。

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.

这篇关于我应该在什么时候做某些SQLite的操作在另一个线程(没有主线程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆