2016-11-21 46 views
0

我在一個android項目上使用ORMLite框架,我在多個repo類上使用OpenHelperManager.getHelper()時卡住了。OpenHelperManager: - 在OpenHelperManager中使用多個幫助器類時設置新的幫助器類ORMLite android

這裏是我的代碼結構的簡單: -

DatabaseHelper類是負責創建databasetables

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{ 

    public static String DATABASE_NAME = "muffet.db"; 
    public static int DATABASE_VERSION = 5; 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { 
     // Creating tables 
     try { 
      TableUtils.createTable(connectionSource, Category.class); 
      TableUtils.createTable(connectionSource, Transaction.class); 
      TableUtils.createTable(connectionSource, Budget.class); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { 
     try { 
      TableUtils.dropTable(connectionSource, Category.class, true); 
      TableUtils.dropTable(connectionSource, Transaction.class, true); 
      TableUtils.dropTable(connectionSource, Budget.class, true); 
      onCreate(database, connectionSource); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

TransactionRepo負責訪問數據庫。

public class TransactionRepo extends DatabaseHelper { 
    // private Dao<Transaction, Integer> transactionDao = null; 
    private RuntimeExceptionDao<Transaction, Integer> transactionRuntimeDao = null; 

    public TransactionRepo(Context context) { 
     super(context); 
    } 

    public RuntimeExceptionDao<Transaction, Integer> getTransactionRuntimeDao(){ 
     if(transactionRuntimeDao == null){ 
      transactionRuntimeDao = getRuntimeExceptionDao(Transaction.class); 
     } 
     return transactionRuntimeDao; 
    } 

    /** 
    * Save Transaction to database 
    */ 
    public void save(Transaction transaction){ 
     try { 
      transactionRuntimeDao = getTransactionRuntimeDao(); 
      transactionRuntimeDao.createOrUpdate(transaction); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

    /** 
    * Get all list of data 
    */ 
    public List<Transaction> getAll(){ 
     try{ 
      transactionRuntimeDao = getTransactionRuntimeDao(); 
      return transactionRuntimeDao.queryForAll(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
      return null; 
     } 
    } 
} 

TransactionServiceImpl這是從TransactionRepo

public class TransactionServiceImpl implements TransactionService { 

    TransactionRepo transactionRepo; 

    @Override 
    public void save(Transaction transaction, Activity activity) { 


     transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class); 
     transactionRepo.save(transaction); 
     OpenHelperManager.releaseHelper(); 
    } 

    @Override 
    public List<Transaction> getAll(Activity activity) { 

     transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class); 

     List<Transaction> transactionList = transactionRepo.getAll(); 

     OpenHelperManager.releaseHelper(); 

     return transactionList; 
    } 
} 

獲取數據的服務類,當我運行它,它工作正常。但是,當我嘗試訪問一些其他repository類擴展DatabaseHelper,並使用OpenHelperManager.getHelper()例如像: -

CategoryRepo categoryRepo = OpenHelperManager.getHelper(activity, CategoryRepo.class); 

我得到錯誤的java.lang.IllegalStateException: Helper class was class com.muffet.persistence.repository.TransactionRepo but is trying to be reset to class com.muffet.persistence.repository.CategoryRepo

這顯然意味着,OpenHelperManager節省的TransactionRepo實例當我嘗試獲取並設置爲CategoryRepo時,它無法投射或類似的東西。

我怎樣才能解決這個問題呢?

謝謝

+0

是不是有任何人誰可以回答這個問題

OpenHelperManager.setOpenHelperClass(CategoryRepo.class);

過嗎?這是幾天,我仍然堅持 – viper

回答

1
public static synchronized void releaseHelper() { 
    instanceCount--; 
    logger.trace("releasing helper {}, instance count = {}", helper, instanceCount); 
    if (instanceCount <= 0) { 
     if (helper != null) { 
      logger.trace("zero instances, closing helper {}", helper); 
      helper.close(); 
      helper = null; 
      wasClosed = true; 
     } 
     if (instanceCount < 0) { 
      logger.error("too many calls to release helper, instance count = {}", instanceCount); 
     } 
    } 
} 

的releasetHelper方法沒有重置助手類靜態會員專區:

private static Class<? extends OrmLiteSqliteOpenHelper> helperClass = null; 

所以,當你想另一個類的調用getHelper,如下表示:

private static void innerSetHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass) { 
    // make sure if that there are not 2 helper classes in an application 
    if (openHelperClass == null) { 
     throw new IllegalStateException("Helper class was trying to be reset to null"); 
    } else if (helperClass == null) { 
     helperClass = openHelperClass; 
    } else if (helperClass != openHelperClass) { 
     throw new IllegalStateException("Helper class was " + helperClass + " but is trying to be reset to " 
       + openHelperClass); 
    } 
} 

因此,即使你以前的helperclass('helperClass')是你以後的helperclass('ope nHelperClass'),但仍然不相等。這是你看到異常的原因。

我的建議是呼籲

CategoryRepo categoryRepo = OpenHelperManager.getHelper(activity, CategoryRepo.class);