在后台线程中打开Sqlite数据库 [英] Opening an Sqlite database in a background thread

查看:209
本文介绍了在后台线程中打开Sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解在Android的后台线程中打开一个Sqlite数据库的意义。现在我通过我的类 DatabaseHelper 为我的数据库使用一个静态/单例模式,所以我只需要打开一次,但我想使用良好的做法打开它,理解为什么我不应该直接从我的 Activity (或者在帮助器的构造函数中)打开它。

I am trying to better understand what it means to open an Sqlite database on a background thread in Android. Right now I am using a static/singleton pattern for my database via my class DatabaseHelper, so I only need to open it once, but I want to open it using good practice and understand why I shouldn't open it directly from within my Activity directly (or within the helper's constructor, for example).

我的类:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static volatile SQLiteDatabase mDatabase;
    private static DatabaseHelper mInstance = null;
    private static Context mContext;

    // ...

    public static synchronized DatabaseHelper getInstance(Context context) {
        /**
         * use the application context as suggested by CommonsWare.
         * this will ensure that you don't accidentally leak an Activity's
         * context (see this article for more information:
         * http://android-developers.blogspot.nl/2009/01/avoiding-memory-leaks.html)
         */
        if (mInstance == null) {
            mInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return mInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE_SOME_TABLE); //some SQL expression
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        db.execSQL(DB_ALTER);
    }

    public void open() throws SQLException {
        mDatabase = getWritableDatabase();
    }

    public void close() {
        mDatabase.close();
    }

    public boolean isOpen() {
        return mDatabase.isOpen();
    }

    //below this would be various CRUD functions operating on mDatabase
    // ...
    // ...
}

这是正确的说,你应该这样做:

Is it correct to say that you should do something like this:

   DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance(this);

   Thread thread = new Thread("OpenDbThread") {
      public void run(){
        mDatabaseHelper.open();
      }
   };


   thread.start();

活动 >

推荐答案

你写的代码在后台线程上打开数据库是正确的。但是,您实际上不会打开数据库,直到 thread.isAlive()返回false(或 mDatabase.isOpen()返回true)。或者,您可以让您的活动侦听来自您的主题的回调。

You're correct that the code you wrote would open the database on a background thread. However, you wouldn't actually the database was opened until thread.isAlive() returned false (or mDatabase.isOpen() returned true). Alternatively, you could make your Activity listen for a callback from your Thread.

这篇关于在后台线程中打开Sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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