Android AsyncTask 和 SQLite 数据库实例
我有一个问题,我不知道如何解决它。我的应用中的一个 Activity 有多个访问单个 SQLiteOpenHelper 的 AsyncTask。我在 onCreate()
中初始化并打开助手,并在 onStop()
中关闭它。我还检查它是否已在 onResume()
中初始化。
自从我发布了我的应用程序以来,我在尝试访问数据库助手的 doInBackground 中收到了许多 Null Exception 错误。我知道发生这种情况是因为在调用 doInBackground 之前数据库已关闭( onStop() ),这很公平。
我的问题是,我应该在哪里关闭数据库连接?在活动中使用数据库助手的单个实例并从多个线程(AsyncTasks
)访问它是否正确?或者我应该为每个 AsyncTask 使用单独的数据库帮助程序实例?
这是我的活动的简化框架:
public class MyActivity extends Activity{
private DbHelper mDbHelper;
private ArrayList<ExampleObject> objects;
@Override
public void onStop(){
super.onStop();
if(mDbHelper != null){
mDbHelper.close();
mDbHelper = null;
}
}
@Override
public void onResume(){
super.onResume();
if(mDbHelper == null){
mDbHelper = new DbHelper(this);
mDbHelper.open();
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
DbHelper mDbHelper = new DbHelper(this);
mDbHelper.open();
}
private class DoSomething extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... arg0) {
objects = mDbHelper.getMyExampleObjects();
return null;
}
@Override
protected void onPostExecute(final Void unused){
//update UI with my objects
}
}
private class DoSomethingElse extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... arg0) {
objects = mDbHelper.getSortedObjects();
return null;
}
@Override
protected void onPostExecute(final Void unused){
//update UI with my objects
}
}
}
I have a problem and I am not sure how to approach it. An activity in my app has multiple AsyncTask
s which access single SQLiteOpenHelper
. I initialize and open the helper in onCreate()
and I am closing it in onStop()
. I also check if it has been initialized in onResume()
.
Since I have published my app I received number of errors with Null Exception in doInBackground
where I try to access the DB helper. I know that this is happens because the DB is closed ( onStop()
) just before the doInBackground
is called, fair enough.
My question is, where should I close the DB connection? Is it right to use a single instance of the DB helper in the Activity and access it from multiple threads(AsyncTasks
)? Or I should use separate DB helper instance for each AsyncTask
?
This is a simplified skeleton of my activity:
public class MyActivity extends Activity{
private DbHelper mDbHelper;
private ArrayList<ExampleObject> objects;
@Override
public void onStop(){
super.onStop();
if(mDbHelper != null){
mDbHelper.close();
mDbHelper = null;
}
}
@Override
public void onResume(){
super.onResume();
if(mDbHelper == null){
mDbHelper = new DbHelper(this);
mDbHelper.open();
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
DbHelper mDbHelper = new DbHelper(this);
mDbHelper.open();
}
private class DoSomething extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... arg0) {
objects = mDbHelper.getMyExampleObjects();
return null;
}
@Override
protected void onPostExecute(final Void unused){
//update UI with my objects
}
}
private class DoSomethingElse extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... arg0) {
objects = mDbHelper.getSortedObjects();
return null;
}
@Override
protected void onPostExecute(final Void unused){
//update UI with my objects
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您不需要管理每个活动的数据库连接。您可以在 android.app.Application 的实例中执行此操作并使用此实例访问数据库。
像这样的事情:
这样你就可以确保你的数据库始终可以访问。
是的,拥有一个 Db helper 实例是一种很好的做法。默认情况下,线程同步是为您进行的。
You don't need to manage db connection for each activity. You can do it in an instance of android.app.Application and access db using this instance.
Something like this:
This way you can be sure you Db is always accessible.
And yes, having one instance of Db helper is a good practice. Thread syncronisation is made for you by default.