更新查詢時出現錯誤。這是我得到的錯誤。領域查詢不更新android
java.lang.IllegalStateException:從不正確的線程訪問域。領域對象只能在創建它們的線程上訪問。
這是我正在使用IntentService代碼:
public void addPatient(Intent intent){
long patientID = intent.getLongExtra("id",0);
Log.v(Constants.TAG, "patientId in datasync " + patientID);
Realm real;
real = Realm.getDefaultInstance();
if(patientID > 0){
Patient patient = realm.where(Patient.class).equalTo("id",patientID).findFirst();
if(patient != null){
JsonObject patientObject = new JsonObject();
patientObject.add("patient",patient.toJsonObject());
Log.v(Constants.TAG, "patientData " + patient.toJsonObject());
try {
// Simulate network access.
Log.e(Constants.TAG, "addPatient: appointment ! =null "+patient);
mNetworkSubscription = NetworkRequest.performAsyncRequest(api.addPatient(patientObject), (data) -> {
// Update UI on main thread
try {
Log.v(Constants.TAG, "result sdsffgfg" + data.getAsJsonObject().get("result"));
if (data.getAsJsonObject().get("error") != null) {
publishResults("addPatient", STATUS_ERROR, null);
}
if (data.getAsJsonObject().get("result") != null) {
int serverID = data.get("result").getAsInt();
Log.v(Constants.TAG, "patientServerId " + serverID);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
patient.setServerID(serverID);
realm.copyToRealmOrUpdate(patient);
Log.v(Constants.TAG, "fdfdfdg " + patient);
}
});
}
} catch (Exception e) {
Log.v(Constants.TAG, "addPatient() exception: " + e.toString());
publishResults("addPatient", STATUS_ERROR, null);
} finally {
publishResults("addPatient", STATUS_FINISHED, null);
}
}, (error) -> {
// Handle Error
Log.e(Constants.TAG, "addPatient Error: " + error.toString());
publishResults("addPatient", STATUS_ERROR, null);
});
} catch (Exception e) {
Log.e(Constants.TAG, "addPatient() Exception: " + e.toString());
publishResults("addPatient", STATUS_ERROR, null);
}
}
}
}
請檢查我要去的地方錯了。我無法保存查詢中的詳細信息
您是否缺少'runOnUiThread()'? –
我應該在哪裏添加? – winchester100
那麼,你的評論說'更新主線程上的UI'。這實際上是在「主線程」上運行嗎? Realm似乎並不這麼認爲 –