小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android – 為單次運(yùn)行應(yīng)用程序只創(chuàng)建一次greenDAO數(shù)據(jù)庫(kù)連接的最佳方法是什么?

 印度阿三17 2019-07-17

目前我正在一個(gè)類(lèi)中創(chuàng)建greenDAO數(shù)據(jù)庫(kù)連接(它在每個(gè)靜態(tài)方法中打開(kāi)連接)并在我需要的地方使用它.但我不確定這是否是最佳方式.
誰(shuí)能建議一個(gè)更好的方法呢?

我的代碼:

import com.knowlarity.sr.db.dao.DaoMaster;
import com.knowlarity.sr.db.dao.DaoMaster.DevOpenHelper;
import com.knowlarity.sr.db.dao.DaoSession;
import com.knowlarity.sr.db.dao.IEntity;

public class DbUtils {

    private static Object lockCallRecord =new Object();
    private DbUtils(){};

    public static boolean saveEntity(Context context , IEntity entity){
            boolean t=false;
            DevOpenHelper helper=null;
            SQLiteDatabase db=null;
            DaoMaster daoMaster=null;
            DaoSession daoSession =null;
            try{
               helper = new DaoMaster.DevOpenHelper(context, IConstant.DB_STRING, null);
               db = helper.getReadableDatabase();
               daoMaster = new DaoMaster(db);
               daoSession = daoMaster.newSession();
               //Some business logic here for fetching and inserting the data.
            }catch (Exception e){
               Log.e("saveEntity", e.getStackTrace().toString());
            }finally{
               if(daoSession!=null)daoSession.clear();
               daoMaster=null;
               if(db.isOpen())db.close();
               helper.close();
            }
            return t;
    }

解決方法:

您的方法導(dǎo)致數(shù)據(jù)庫(kù)經(jīng)常被加載,這是不必要的,可能會(huì)顯著減慢您的應(yīng)用程序.

打開(kāi)數(shù)據(jù)庫(kù)一次并將其存儲(chǔ)在某處,并在需要時(shí)從那里請(qǐng)求它.

我個(gè)人使用全球DaoSession和本地DaoSessions.本地DaoSessions被用于會(huì)話緩存中不應(yīng)該保留任何內(nèi)容(即將新對(duì)象持久存儲(chǔ)到數(shù)據(jù)庫(kù)中,這可能只是非常罕見(jiàn)地使用或者執(zhí)行一些查詢會(huì)加載很多不太可能再次被重用的實(shí)體).

請(qǐng)記住,如果您在全局會(huì)話中使用該實(shí)體,那么更新本地DaoSession中的實(shí)體也是一個(gè)壞主意.如果這樣做,全局會(huì)話中的緩存實(shí)體將不會(huì)更新,除非您清除全局會(huì)話的緩存,否則您將得到錯(cuò)誤的結(jié)果!

因此,最安全的方法是始終只使用一個(gè)DaoSession或新的DaoSessions并且不使用全局和本地會(huì)話!

自定義應(yīng)用程序類(lèi)是個(gè)好地方,但任何其他類(lèi)也都可以.

我是這樣做的:

class DBHelper:

private SQLiteDatabase _db = null;
private DaoSession _session = null;

private DaoMaster getMaster() {
    if (_db == null) {
        _db = getDatabase(DB_NAME, false);
    }
    return new DaoMaster(_db);
}

public DaoSession getSession(boolean newSession) {
    if (newSession) {
        return getMaster().newSession();
    }
    if (_session == null) {
        _session = getMaster().newSession();
    }
    return _session;
}

private synchronized SQLiteDatabase getDatabase(String name, boolean readOnly) {
    String s = "getDB("   name   ",readonly="   (readOnly ? "true" : "false")   ")";
    try {
        readOnly = false;
        Log.i(TAG, s);
        SQLiteOpenHelper helper = new MyOpenHelper(context, name, null);
        if (readOnly) {
            return helper.getReadableDatabase();
        } else {
            return helper.getWritableDatabase();
        }
    } catch (Exception ex) {
        Log.e(TAG, s, ex);
        return null;
    } catch (Error err) {
        Log.e(TAG, s, err);
        return null;
    }
}

private class MyOpenHelper extends DaoMaster.OpenHelper {
    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "Create DB-Schema (version " Integer.toString(DaoMaster.SCHEMA_VERSION) ")");
        super.onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "Update DB-Schema to version: " Integer.toString(oldVersion) "->" Integer.toString(newVersion));
        switch (oldVersion) {
            case 1:
                db.execSQL(SQL_UPGRADE_1To2);
            case 2:
                db.execSQL(SQL_UPGRADE_2To3);
                break;
            default:
                break;
        }
    }
}

在應(yīng)用程序類(lèi)中:

private static MyApplication _INSTANCE = null;

public static MyApplication getInstance() {
    return _INSTANCE;
}

@Override
public void onCreate() {
    _INSTANCE = this;
    // ...
}

private DBHelper _dbHelper = new DBHelper();

public static DaoSession getNewSession() {
    return getInstance()._dbHelper.getSession(true);
}

public static DaoSession getSession() {
    return getInstance()._dbHelper.getSession(false);
}

當(dāng)然,您也可以存儲(chǔ)DaoMaster而不是DB本身.這將減少一些小的開(kāi)銷(xiāo).

我每次使用一些常用方法(比如訪問(wèn)數(shù)據(jù)庫(kù))時(shí)都使用類(lèi)似Singleton的Application類(lèi)和靜態(tài)方法來(lái)避免轉(zhuǎn)換應(yīng)用程序(((MyApplication)getApplication())).

來(lái)源:https://www./content-2-336601.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多