0
在我的应用程序中,我通过后台线程中的领域事务同步所有数据。但是,偶尔当我尝试在UI线程中检索最近同步的对象时,我得到一个空指针异常。我写了一个例程,似乎是解决这个小延迟问题的一种解决方案,但它感觉就像一个“哈克”解决方案。任何指导将不胜感激。我已经发布了以下例程的简化版本。在后台线程中写入和在UI线程中检索对象之间的域延迟?
private void attemptToEnterActivity(){
// Retrieve the object's key.
String key = Application.getInstance().getRecentlySyncedPrimaryKey();
// Retrieve the realm object with the key.
Realm realm = Realm.getDefaultInstance();
Object object = realm.where(Object.class)
.equalTo("key", key)
.findFirst();
if (object != null) {
new NavigationController(this).activity_start(new Intent(this, Activity.class));
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
attemptToEnterActivity();
}
}, 200);
}
}