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);
}
}