我想executeTransactionAsync在执行完毕后关闭我的Realm实例.原因是我的应用程序主线程一直在冻结,我认为它的原因是后台域实例在执行完成后没有被关闭.请参阅下面的代码:
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Execute realm code
realm.copyToRealmOrUpdate(myData);
// Can I close the realm instance here without getting an
// error? realm.close(); causes an error.
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.i("CB", "success");
// looks like I cannot access the execute Realm
// instance here.
// Closing realm.getDefaultInstance does not change my issue
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.i("CB", "error - " + error.getMessage());
}
});
}
Run Code Online (Sandbox Code Playgroud)
请看我的评论.我的应用程序屏幕变黑了.执行成功完成onSuccess()并被调用,但我无法访问execute领域实例以从此处关闭它.
你有什么建议可以尝试吗?难道我做错了什么?
先感谢您.
编辑
07-19 11:43:42.379 8146-8146/com.shortterminsurance.shortterm I/CB: success
07-19 11:43:43.258 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 33.234ms
07-19 11:43:43.266 8146-8156/com.shortterminsurance.shortterm I/art: Background partial concurrent mark sweep GC freed 476307(17MB) AllocSpace objects, 512(10MB) LOS objects, 40% free, 33MB/55MB, paused 7.261ms total 163.497ms
07-19 11:43:44.131 8146-8156/com.shortterminsurance.shortterm I/art: Background sticky concurrent mark sweep GC freed 408160(9MB) AllocSpace objects, 459(15MB) LOS objects, 35% free, 35MB/55MB, paused 10.287ms total 147.823ms
07-19 11:43:44.834 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 103.676ms
07-19 11:43:44.848 8146-8156/com.shortterminsurance.shortterm W/art: Suspending all threads took: 13.424ms
Run Code Online (Sandbox Code Playgroud)
这是我的onSuccess被调用后的logcat.我认为领域的后台实例execute因某些原因而继续运行:(.
Tim*_*Tim 10
在这里传递的领域实例
@Override
public void execute(Realm realm) {
Run Code Online (Sandbox Code Playgroud)
对你来说是封闭的.您不必担心自己关闭它.
如果您检查Realm.executeTransactionAsync()的源代码,您可以找到
bgRealm.beginTransaction();
try {
transaction.execute(bgRealm);
if (!Thread.currentThread().isInterrupted()) {
bgRealm.commitTransaction(false, new Runnable() {
@Override
public void run() {
// The bgRealm needs to be closed before post event to caller's handler to avoid
// concurrency problem. eg.: User wants to delete Realm in the callbacks.
// This will close Realm before sending REALM_CHANGED.
bgRealm.close();
}
});
transactionCommitted = true;
}
}
...
Run Code Online (Sandbox Code Playgroud)
所以首先它调用.execute()你的事务,然后关闭它传递给你的域实例.
| 归档时间: |
|
| 查看次数: |
1759 次 |
| 最近记录: |