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

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

问题描述

我有一个问题,我不知道如何处理它。在我的应用程序的活动有多个的AsyncTask ■哪些访问单个 SQLiteOpenHelper 。我初始化并打开的onCreate()的帮助,我关闭它在的onStop()。我也检查,如果它在 onResume被初始化()

自从我发表了我的应用我在 doInBackground ,我尝试访问DB助手收到零异常错误的数量。我知道,这是是因为数据库被关闭(的onStop())之前的 doInBackground 被调用,公平够了。

我的问题是,我应该关闭数据库连接?那么,是否可以使用DB助手的单个实例中的活动,并从多个线程访问(AsyncTasks <$ C C $>)?或者我应该使用单独的数据库实例,帮助每个的AsyncTask

这是我的活动的简化骨架:

 公共类MyActivity延伸活动{
    私人DbHelper mDbHelper;
    私人的ArrayList&LT; ExampleObject&GT;对象;

    @覆盖
    公共无效的onStop(){
        super.onStop();
        如果(mDbHelper!= NULL){
            mDbHelper.close();
            mDbHelper = NULL;
        }
    }

    @覆盖
    公共无效onResume(){
        super.onResume();
        如果(mDbHelper == NULL){
            mDbHelper =新DbHelper(本);
            mDbHelper.open();
        }
    }

    @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);
        DbHelper mDbHelper =新DbHelper(本);
        mDbHelper.open();
    }

    私有类DoSomething的扩展AsyncTask的&LT;字符串,太虚,太虚&GT; {

        @覆盖
        保护无效doInBackground(字符串...为arg0){
            对象= mDbHelper.getMyExampleObjects();
            返回null;
        }

        @覆盖
        保护无效onPostExecute(最终虚空未使用){
            //更新UI与我的对象
        }
    }

    私有类DoSomethingElse扩展的AsyncTask&LT;字符串,太虚,太虚&GT; {

        @覆盖
        保护无效doInBackground(字符串...为arg0){
            对象= mDbHelper.getSortedObjects();
            返回null;
        }

        @覆盖
        保护无效onPostExecute(最终虚空未使用){
            //更新UI与我的对象
        }
    }
}
 

解决方案

您提到您关闭数据库之前取消AsyncTask的。但是你应该记住,取消的AsyncTask只是信号的任务被取消,你需要检查isCancelled()在doInBackground()和做要紧停止数据库操作。

在关闭数据库,然后需要检查的getStatus(),以确保该AsyncTask的已停止。

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().

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

解决方案

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.

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

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

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