2013-02-17 54 views
1

我嘗試遍歷ForeignCollection<OrderItems>。這項工作如果eager設置爲true但惰性選項仍然無法正常工作。通過ForeignCollection遍歷的ORMLite拋出異常

首先,我讓

OrderDataSource orderDS = new OrderDataSource(getAppContext()); 
ws.Order order = orderDS.convertToWS(mOrder,ws.Order.class); 
orderDS.close(); 

close()關閉數據庫連接和convertToWS()做迭代。的convertToWS()

部分:

Iterator<OrderItem> itr = order.getOrderItems().iterator(); 

while (itr.hasNext()) 
{ 
    OrderItem orderItem = itr.next(); 
    ... 
} 

close方法是從convertToWS()返回後調用。我與convertToWS()中的其他對象一起工作有任何問題。

我也嘗試使用CloseableIterator並在finally區塊中關閉它,但沒有成功。期待這一點,我不關閉連接。

例外:

02-17 02:21:43.094: E/AndroidRuntime(4820): FATAL EXCEPTION: main 
02-17 02:21:43.094: E/AndroidRuntime(4820): java.lang.IllegalStateException: database /data/data/my_app/databases/my_db (conn# 0) already closed 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1556) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:162) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:232) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.BaseDaoImpl.createIterator(BaseDaoImpl.java:933) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.BaseDaoImpl.iterator(BaseDaoImpl.java:531) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.BaseDaoImpl.iterator(BaseDaoImpl.java:526) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.LazyForeignCollection.seperateIteratorThrow(LazyForeignCollection.java:74) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.LazyForeignCollection.iteratorThrow(LazyForeignCollection.java:60) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.LazyForeignCollection.closeableIterator(LazyForeignCollection.java:50) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.LazyForeignCollection.iterator(LazyForeignCollection.java:45) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.j256.ormlite.dao.LazyForeignCollection.iterator(LazyForeignCollection.java:26) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at orders.OrderDataSource.convertToWS(OrderDataSource.java:409) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at fragments.OrderDetailFragment.sendOrder(OrderDetailFragment.java:419) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at fragments.OrderDetailFragment.doPositiveClick(OrderDetailFragment.java:347) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at ui.dialogfragments.AlertDialogFragment$1.onClick(AlertDialogFragment.java:54) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.os.Looper.loop(Looper.java:137) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-17 02:21:43.094: E/AndroidRuntime(4820):  at dalvik.system.NativeStart.main(Native Method) 
+0

當您進行熱切獲取時會發生什麼? – smk 2013-02-17 03:36:04

+0

我犯了一個錯誤,渴望工作正確。 – misco 2013-02-17 04:15:07

回答

2

你需要有數據庫,以遍歷值,因爲你正在做懶查詢開放的,這意味着你是從數據庫中的第一次訪問對象獲取數據來自外國收藏。

因此,如果您要擴展ORMLiteActivity,那麼您的數據庫將保持打開狀態,直到您銷燬該屏幕。

這裏最主要的是在你使用數據時保持助手打開。

+0

我已經更新了我的問題,我添加了關於迭代的詳細信息。 – misco 2013-02-17 03:28:32

+1

所以當你渴望拉你從數據庫中獲取所有數據。該連接將被釋放,並且您將繼續使用內存中的對象。在進行延遲加載時,只會獲得內存中的部分數據,並在您開始訪問集合中的數據時繼續完成其餘的工作。作爲例外狀態,當您嘗試迭代時,數據庫將關閉,從而阻止將其餘數據拉到內存中。我不知道爲什麼會發生這種情況,但我的建議是,看看是否可能在迭代之前以某種方式釋放助手。 – DArkO 2013-02-17 04:45:20