gpt4 book ai didi

android - SQLiteOpenHelper 同步

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:31 28 4
gpt4 key购买 nike

所以我想出了一些想法,我想知道它是否可以实现。

假设我有多个表(数据库模型),每个表都由某个类表示。我不习惯将单例模式与 open helper 一起使用,所以我创建了一些简单的类来提供数据库。我的想法是,只要所有表都引用 SQLiteDatabase(由 open helper 返回),它们都将使用同一个数据库实例,并且可能不需要将工作与数据库同步,因为 open helper 做这个。当最后一个表完成它的工作时,GC 将收集打开的帮助程序(因为最后一个引用将是弱引用)-> finalize() 被调用,我在此方法期间关闭数据库以防止操作系统发出任何警告。我的问题是:这可行吗?它会自动关闭数据库吗?它会泄漏或抛出一些异常吗?

这是我的类(class):

public class DatabaseHelper {

private static WeakReference<SomeCustomOpenHelper> sDBOpenHelper;

private void notifyDBCreate(SQLiteDatabase db) {
for (DBTable table : mTables) {
table.onDBCreate(db);
}
}

private void notifyDBUpgrade(SQLiteDatabase db) {
for (DBTable table : mTables) {
table.onDBUpgrade(db);
}
}

public SQLiteDatabase getDatabase(boolean readOnly) {
SomeCustomOpenHelper dbHelper = sDBOpenHelper.get();
if (dbHelper == null) {
dbHelper = new SomeCustomOpenHelper(context, name, factory, version, new DatabaseEventsCallback());
sDBOpenHelper = new WeakReference<SomeCustomOpenHelper>(dbHelper);
}
if (readOnly) {
return dbHelper.getReadableDatabase();
} else {
return dbHelper.getWritableDatabase();
}
}

private class DatabaseEventsCallback implements IDatabaseEventsCallback {

@Override
public void onCreate(SQLiteDatabase db) {
notifyDBCreate(db);
}

@Override
public void onUpgrade(SQLiteDatabase db) {
notifyDBUpgrade(db);
}

}

interface IDatabaseEventsCallback {
void onCreate(SQLiteDatabase db);

void onUpgrade(SQLiteDatabase db);
}

private static class SomeCustomOpenHelper extends SQLiteOpenHelper {

private IDatabaseEventsCallback mCB;

public SomeCustomOpenHelper(Context context, String name, CursorFactory factory, int version, IDatabaseEventsCallback cb) {
super(context, name, factory, version);

mCB = cb;
}

@Override
public void onCreate(SQLiteDatabase db) {
mCB.onCreate(db);
}

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

@Override
protected void finalize() throws Throwable {
this.close();
super.finalize();
}
}
}

最佳答案

其实也不知道答案,但是有兴趣查了一下。

答案在这里写得很好; http://blog.foxxtrot.net/2009/01/a-sqliteopenhelper-is-not-a-sqlitetablehelper.html

但基本上信息的核心是;

I created three SQLiteOpenHelper classes, one for each table, even though they all referenced only a single database file.

Here is where everything fell apart. Android maintains Versions for databases based on the package it’s associated with, the name of the database, and the version number you provide. The package and name go into decided what the path on the device will be, while the version is stored (somewhere) on the device so that it knows when it needs to call an OpenHelper’s onUpgrade event handler. It turns out that if, in the SQLiteOpenHelper Constructor, it determines that the database already exists, it won’t call your onCreate or onUpgrade methods at all, even if the particular class which is making the call has never been called before.

关于android - SQLiteOpenHelper 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15654745/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com