乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       印度阿三17 2019-07-17

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

      我的代碼:

      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ù)庫經(jīng)常被加載,這是不必要的,可能會顯著減慢您的應(yīng)用程序.

      打開數(shù)據(jù)庫一次并將其存儲在某處,并在需要時從那里請求它.

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

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

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

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

      我是這樣做的:

      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)用程序類中:

      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)然,您也可以存儲DaoMaster而不是DB本身.這將減少一些小的開銷.

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

      來源:https://www./content-2-336601.html

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多