【发布时间】:2016-06-28 19:26:39
【问题描述】:
在我的应用程序中,我通过后台线程中的领域事务同步所有数据。但是,有时当我尝试在 UI 线程中检索最近同步的对象时,会收到空指针异常。我编写了一个例程,似乎可以解决这个小延迟问题,但感觉就像一个“hacky”解决方案。任何指导将不胜感激。我在下面发布了一个简化版的例程。
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);
}
}
【问题讨论】:
标签: android multithreading background realm