发生错误的我的代码片段:
private void setupList() {
if (mRealm.isClosed()) {
return;
}
mResult = mRealm.where(X.class)
.equalTo(...)
.findAllAsync();
mResult.addChangeListener(mListener);
}
错误:
Fatal Exception: java.lang.IllegalStateException: Cannot create asynchronous query while in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 252
at io.realm.internal.OsResults.nativeStartListening(OsResults.java)
at io.realm.internal.OsResults.addListener(OsResults.java:405)
我了解到不同SO主题中的类似错误,并得出结论,如果不提交事务,我将无法addChangeListener
。这是否意味着我可以简单地将beginTransaction
和commitTransaction
放在mResult = mRealm.where...
之前或之后,还是在尝试异步查找结果时必须以不同的方式进行处理?