2017-08-28 61 views
1

更新查詢時出現錯誤。這是我得到的錯誤。領域查詢不更新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); 
      } 
     } 
    } 
} 

請檢查我要去的地方錯了。我無法保存查詢中的詳細信息

+0

您是否缺少'runOnUiThread()'? –

+0

我應該在哪裏添加? – winchester100

+0

那麼,你的評論說'更新主線程上的UI'。這實際上是在「主線程」上運行嗎? Realm似乎並不這麼認爲 –

回答

2
public void addPatient(final long patientId){ 
    try(Realm realm = 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()); 

       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 { 
          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); 
           try(Realm r = Realm.getDefaultInstance()) { 
            r.executeTransaction((_realm) -> { 
             Patient _patient = _realm.where(Patient.class).equalTo("id", patientId).findFirst(); 
             _patient.setServerId(serverId); 
            }); 
           } 
          } 
         } 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); 
       } 
      } 
     } 
    } 
} 
+0

正如我在評論中暗示的那樣,需要在'performAsyncRequest'內調用'Realm.getDefaultInstance()',你已經完成 –

+0

是的,但是他還需要在主線程上查詢對象:D – EpicPandaForce

+0

@EpicPandaForce :謝謝你的伴侶,你救了我至少2天的工作。我被打了很久 – winchester100