I currently use Loaders to grab data from my ContentProvider (to enable auto-updating of my Cursors). This approach is straight-forward for Querying the database, though, it seems ill suited for any other DB operation (such as Insert, Update, Delete).
我目前使用Loaders从我的ContentProvider中获取数据(以启用我的游标的自动更新)。这种方法对于查询数据库是直截了当的,但它似乎不适合任何其他数据库操作(例如插入,更新,删除)。
My questions are:
我的问题是:
- Do all SQLite operations need to be on a background thread, or is it safe to do simple operations like Inserting, Updating, or Deleting a single row on the UI thread?
- 是否所有SQLite操作都需要在后台线程上,或者在UI线程上执行插入,更新或删除单行等简单操作是否安全?
- What is a nice design patter to ensure all queries go through a background thread? I would like to implement AsyncTask, should I create a SuperTask so to speak that extends AsyncTask and Executes each SQLite operation? (Bonus: Can you provide bare-bones example?)
- 什么是一个很好的设计模式,以确保所有查询都通过后台线程?我想实现AsyncTask,我应该创建一个SuperTask,以便扩展AsyncTask并执行每个SQLite操作吗? (额外奖励:你能提供一些简单的例子吗?)
Thank you!
谢谢!
2 个解决方案
#1
5
I have done SQLite operations on my UI Thread. I guess the question really becomes whether your queries will ever take a long time or not. I've never had my application crash from taking too long to execute SQL calls on my SQLite database.
我在UI线程上完成了SQLite操作。我想问题真的是你的查询是否会花费很长时间。我的SQLite数据库执行SQL调用花了太长时间,我的应用程序崩溃了。
With that said, if you plan on writing complex queries that can take time to load you would want to run it as an AsyncTask or Thread and use callbacks to update your UI if need be.
话虽如此,如果您计划编写可能需要时间加载的复杂查询,您可能希望将其作为AsyncTask或Thread运行,并在需要时使用回调来更新UI。
This is a great tutorial on SQLite on Android (It also addresses some of the complex sql timing issues you were talking about): http://www.vogella.com/tutorials/AndroidSQLite/article.html
这是关于Android上SQLite的一个很棒的教程(它还解决了你所讨论的一些复杂的sql时序问题):http://www.vogella.com/tutorials/AndroidSQLite/article.html
#2
2
-
All SQLite operations do not need to be on a background, but should be. Even simple row updates can impact the UI thread and therefore application responsiveness.
所有SQLite操作都不需要在后台运行,但应该是。即使是简单的行更新也会影响UI线程,从而影响应用程序响应能力。
-
Android includes the AsyncQueryHandler abstract class:
Android包含AsyncQueryHandler抽象类:
A helper class to help make handling asynchronous ContentResolver queries easier.
一个帮助程序类,可帮助您更轻松地处理异步ContentResolver查询。
Here are two example implementations from Using AsyncQueryHandler to Access Content Providers Asynchronously in Android. A member class:
以下是在Android中使用AsyncQueryHandler和异步访问内容提供程序的两个示例实现。会员类:
class MyQueryHandler extends AsyncQueryHandler {
public MyQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// query() completed
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
// insert() completed
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
// update() completed
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
// delete() completed
}
}
An anonymous class:
一个匿名类:
AsyncQueryHandler queryHandler = new AsyncQueryHandler(getContentResolver()) {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor == null) {
// Some providers return null if an error occurs whereas others throw an exception
}
else if (cursor.getCount() < 1) {
// No matches found
}
else {
while (cursor.moveToNext()) {
// Use cursor
}
}
}
};
Further details:
更多细节:
-
Implementing AsyncQueryHandler
实现AsyncQueryHandler
-
http://www.trustydroid.com/blog/2014/10/07/using-asyncqueryhandler-with-content-provider/
http://www.trustydroid.com/blog/2014/10/07/using-asyncqueryhandler-with-content-provider/
#1
5
I have done SQLite operations on my UI Thread. I guess the question really becomes whether your queries will ever take a long time or not. I've never had my application crash from taking too long to execute SQL calls on my SQLite database.
我在UI线程上完成了SQLite操作。我想问题真的是你的查询是否会花费很长时间。我的SQLite数据库执行SQL调用花了太长时间,我的应用程序崩溃了。
With that said, if you plan on writing complex queries that can take time to load you would want to run it as an AsyncTask or Thread and use callbacks to update your UI if need be.
话虽如此,如果您计划编写可能需要时间加载的复杂查询,您可能希望将其作为AsyncTask或Thread运行,并在需要时使用回调来更新UI。
This is a great tutorial on SQLite on Android (It also addresses some of the complex sql timing issues you were talking about): http://www.vogella.com/tutorials/AndroidSQLite/article.html
这是关于Android上SQLite的一个很棒的教程(它还解决了你所讨论的一些复杂的sql时序问题):http://www.vogella.com/tutorials/AndroidSQLite/article.html
#2
2
-
All SQLite operations do not need to be on a background, but should be. Even simple row updates can impact the UI thread and therefore application responsiveness.
所有SQLite操作都不需要在后台运行,但应该是。即使是简单的行更新也会影响UI线程,从而影响应用程序响应能力。
-
Android includes the AsyncQueryHandler abstract class:
Android包含AsyncQueryHandler抽象类:
A helper class to help make handling asynchronous ContentResolver queries easier.
一个帮助程序类,可帮助您更轻松地处理异步ContentResolver查询。
Here are two example implementations from Using AsyncQueryHandler to Access Content Providers Asynchronously in Android. A member class:
以下是在Android中使用AsyncQueryHandler和异步访问内容提供程序的两个示例实现。会员类:
class MyQueryHandler extends AsyncQueryHandler {
public MyQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// query() completed
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
// insert() completed
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
// update() completed
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
// delete() completed
}
}
An anonymous class:
一个匿名类:
AsyncQueryHandler queryHandler = new AsyncQueryHandler(getContentResolver()) {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor == null) {
// Some providers return null if an error occurs whereas others throw an exception
}
else if (cursor.getCount() < 1) {
// No matches found
}
else {
while (cursor.moveToNext()) {
// Use cursor
}
}
}
};
Further details:
更多细节:
-
Implementing AsyncQueryHandler
实现AsyncQueryHandler
-
http://www.trustydroid.com/blog/2014/10/07/using-asyncqueryhandler-with-content-provider/
http://www.trustydroid.com/blog/2014/10/07/using-asyncqueryhandler-with-content-provider/