1

我尝试迭代ForeignCollection<OrderItems>。如果eager设置为,则此方法有效,true但惰性选项仍然无效。

首先,我做

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();
    ...
}

从返回后调用方法关闭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)
4

1 回答 1

2

您需要打开数据库才能迭代值,因为您正在执行惰性查询,这意味着您第一次从外部集合访问对象时从数据库中获取数据。

因此,如果您正在扩展 ORMLiteActivity,那么您的数据库将保持打开状态,直到您销毁该屏幕。

这里的主要内容是在您使用数据时保持助手打开。

于 2013-02-17T03:10:43.353 回答