对 SQLiteDatabase 使用单例设计模式 [英] Using Singleton design pattern for SQLiteDatabase

查看:40
本文介绍了对 SQLiteDatabase 使用单例设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,我正在开发一个简单的应用程序以获得一些基本体验.我的应用程序非常简单,其中包括广播接收器和一些活动.两个组件都使用同一个数据库,因此理论上可能会同时尝试访问数据库.

目前我只是在每次需要时实例化 db 对象(它是一个 SQLite db helper 类),并执行所需的操作:查询、插入等.

从我在这里和其他一些文档中阅读的内容来看,如果同时访问数据库,则会出现数据库锁定"异常的问题,因此更好的方法是拥有此数据库的单个实例对象,因此所有组件始终使用相同的数据库连接.

以上推理是否正确?那么单身人士会是一个足够好的解决方案吗?我知道有些纯粹主义者可能会反对它,但请注意,这是一个相当简单的应用程序,因此我可以负担得起在其他情况下不会做的事情.

否则,更好的选择是什么?我已经阅读了有关使用内容提供程序的信息,但是这太过分了,此外我没有兴趣与其他活动共享数据.我确实读过这篇帖子 并发现它很有帮助.

解决方案

点击在这里查看我关于这个主题的博文.

<小时>

以下是一些示例代码,说明了三种可能的方法.这些将允许在整个应用程序中访问数据库.

方法#1:让`SQLiteOpenHelper` 成为静态数据成员

这不是完整的实现,但它应该让您对如何正确设计 DatabaseHelper 类有一个很好的了解.静态工厂方法确保在任何时候都只存在一个 DatabaseHelper 实例.

/*** 创建扩展 SQLiteOpenHelper 的自定义 DatabaseHelper 类*/公共类 DatabaseHelper 扩展 SQLiteOpenHelper {私有静态 DatabaseHelper mInstance = null;private static final String DATABASE_NAME = "databaseName";private static final String DATABASE_TABLE = "tableName";私有静态最终 int DATABASE_VERSION = 1;私有上下文 mCxt;公共静态 DatabaseHelper getInstance(Context ctx) {/*** 使用 CommonsWare 建议的应用程序上下文.* 这将确保您不会不小心泄露一个 Activity* 上下文(有关更多信息,请参阅本文:* http://android-developers.blogspot.nl/2009/01/avoiding-memory-leaks.html)*/如果(mInstance == null){mInstance = new DatabaseHelper(ctx.getApplicationContext());}返回 mInstance;}/*** 构造函数应该是私有的,以防止直接实例化.* 改为调用静态工厂方法getInstance()".*/私人数据库助手(上下文 ctx){超级(上下文,DATABASE_NAME,null,DATABASE_VERSION);this.mCtx = ctx;}}

方法#2:使用`ContentProvider`抽象SQLite数据库

这是我建议的方法.一方面,新的 CursorLoader 类需要 ContentProviders,所以如果你想要一个 Activity 或 Fragment 来实现 LoaderManager.LoaderCallbacksCursorLoader(我建议你利用它,它很神奇!),你需要为你的应用程序实现一个 ContentProvider.此外,您无需担心使用 ContentProvider 制作单例数据库助手.只需从 Activity 中调用 getContentResolver(),系统就会为您处理一切(换句话说,无需设计单例模式来防止创建多个实例).>

希望这有帮助!

I'm rather newbie on Android, and I'm working on a simple application to get some basic experience. My app is pretty simple and consists among other things of a broadcast receiver and some activities. Both components make use of a single database, so in theory it could happen that both try to access the db concurrently.

Currently I'm simply instantiating the db object (which is-a SQLite db helper class) each time I need it, and performing the needed operations: query, insert, etc.

From what I've been reading here and in some other documents, this has the problem of getting a "db locked" exception in case the db is accessed concurrently, so a better approach would be having a single instance of this db object so all components use the same db connection at all times.

Is the above reasoning correct? Would a singleton then be a good-enough solution for this? I know some purists may argue against it, but please note that this is a rather simple application so I can afford doing things I wouldn't in other cases.

Otherwise, what would a better option be? I've read about using content provider but it would be too much for this, besides that I'm not interested to share the data with other activities. I have indeed read this post and found it rather helpful.

解决方案

Click here to see my blog post on this subject.


Here is some sample code that illustrates three possible approaches. These will allow access to the database throughout the application.

Approach #1: have `SQLiteOpenHelper` be a static data member

This isn't the complete implementation, but it should give you a good idea on how to go about designing the DatabaseHelper class correctly. The static factory method ensures that there exists only one DatabaseHelper instance at any time.

/**
 * create custom DatabaseHelper class that extends SQLiteOpenHelper
 */
public class DatabaseHelper extends SQLiteOpenHelper { 
    private static DatabaseHelper mInstance = null;

    private static final String DATABASE_NAME = "databaseName";
    private static final String DATABASE_TABLE = "tableName";
    private static final int DATABASE_VERSION = 1;

    private Context mCxt;

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

    /**
     * constructor should be private to prevent direct instantiation.
     * make call to static factory method "getInstance()" instead.
     */
    private DatabaseHelper(Context ctx) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mCtx = ctx;
    }
}

Approach #2: abstract the SQLite database with a `ContentProvider`

This is the approach I would suggest. For one, the new CursorLoader class requires ContentProviders, so if you want an Activity or Fragment to implement LoaderManager.LoaderCallbacks<Cursor> with a CursorLoader (which I suggest you take advantage of, it is magical!), you'll need to implement a ContentProvider for your application. Further, you don't need to worry about making a Singleton database helper with ContentProviders. Simply call getContentResolver() from the Activity and the system will take care of everything for you (in other words, there is no need for designing a Singleton pattern to prevent multiple instances from being created).

Hope this helps!

这篇关于对 SQLiteDatabase 使用单例设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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