Android AsyncTask 和 SQLite 数据库实例 [英] Android AsyncTask and SQLite DB instance

查看:19
本文介绍了Android AsyncTask 和 SQLite 数据库实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,不知道如何解决.我的应用程序中的 Activity 有多个 AsyncTask 可以访问单个 SQLiteOpenHelper.我在 onCreate() 中初始化并打开 helper,然后在 onStop() 中关闭它.我还检查它是否已在 onResume() 中初始化.

I have a problem and I am not sure how to approach it. An activity in my app has multiple AsyncTasks 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().

自从我发布了我的应用程序后,我在尝试访问 DB 帮助程序的 doInBackground 中收到了一些带有 Null Exception 的错误.我知道这是因为数据库在 doInBackground 被调用之前关闭( onStop() ),很公平.

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.

我的问题是,我应该在哪里关闭数据库连接?在 Activity 中使用 DB 助手的单个实例并从多个线程(AsyncTasks)访问它是否正确?或者我应该为每个 AsyncTask 使用单独的数据库助手实例?

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
        }
    }
}

推荐答案

您提到在关闭数据库之前取消了 AsyncTask.但您应该记住,取消 AsyncTask 只是表示要取消的任务,您需要检查 doInBackground() 中的 isCancelled() 并执行停止数据库操作所需的操作.

You mentioned that you cancel the AsyncTask before closing the DB. But you should keep in mind that cancelling the AsyncTask just signals the task to be cancelled and you need to check isCancelled() in doInBackground() and do the needful to stop DB operations.

在关闭数据库之前,您需要检查 getStatus() 以确保 AsyncTask 已停止.

Before closing the DB, you then need to check getStatus() to be sure that the AsyncTask has stopped.

这篇关于Android AsyncTask 和 SQLite 数据库实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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