我想在执行完成后在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());
}
});
}
请参阅我的评论。我的应用程序屏幕变黑了。执行成功完成并调用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
这是我的onSuccess被调用后的logcat。我认为execute
中的域的后台实例由于某种原因而继续运行:(。
答案 0 :(得分:10)
此处传递的领域实例
@Override
public void execute(Realm realm) {
对您关闭。你不必担心自己关闭它。
如果检查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;
}
}
...
首先,它会在您的事务上调用.execute()
,然后关闭它传递给您的域实例。
答案 1 :(得分:2)
这是一种可能性:
final Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Execute realm code
realm.copyToRealmOrUpdate(myData);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.i("CB", "success");
realm.close();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.i("CB", "error - " + error.getMessage());
realm.close();
}
});
}
虽然后台领域实例会自动关闭。
答案 2 :(得分:1)
正如Tim Castelijns所说,executeTransactionAsync已经关闭了领域,无论如何,不关闭领域的实例不会导致UI阻塞,但是如果你试图打开一个Realm实例会引发异常,所以你应该有另一个问题
答案 3 :(得分:0)
final Realm realmFirstInstance = Realm.getDefaultInstance();
realmFirstInstance.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Execute realm code
realm.copyToRealmOrUpdate(myData);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.i("CB", "success");
realmFirstInstance.close();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.i("CB", "error - " + error.getMessage());
realmFirstInstance.close();
}
});
}
此外,为了更清晰地理解:不要忘记关闭第一个领域实例。因为在这里execute()方法中的“ realmFirstInstance”和“ realm”是两个不同的对象。