2015-12-16 50 views
1

我正在嘗試更新我的RelamObject(本例中是RealmCase)中的一個屬性。我已經嘗試了下面的代碼,它看起來像我的Realm中沒有更新,雖然我在RealmPatientInfo表中看到了一個新對象,但是我的RealmCase沒有創建任何關係。在Realm中執行更新時出錯

RealmCase realmCase = new RealmCase(); 
    realmCase.setId(getId()); 
    realmCase.setPatientInfo(new RealmPatientInfo(patientInfo)); 
    Realm realm = Realm.getDefaultInstance(); 
    realm.beginTransaction(); 
    realm.copyToRealmOrUpdate(realmCase); 
    realm.commitTransaction(); 
    realm.close(); 

我也嘗試了以下,但得到一個異常,說價值不是由領域管理。

Realm realm = Realm.getDefaultInstance(); 
    realm.beginTransaction(); 
    RealmQuery<RealmCase> query = realm.where(RealmCase.class); 
    RealmCase persistedCase = query.findFirst(); 
    persistedCase.setPatientInfo(new RealmPatientInfo(patientInfo)); 
    realm.copyToRealmOrUpdate(realmCase); 
    realm.commitTransaction(); 
    realm.close(); 

我也想刪除舊patientInfo對象(參考和在RealmPatientInfo表中的條目),下面是我在這個嘗試,儘管從我之前錯誤使我從測試的一部分。

 Realm realm = Realm.getDefaultInstance(); 
     realm.beginTransaction(); 
     RealmQuery<RealmCase> query = realm.where(RealmCase.class); 
     RealmCase persistedCase = query.findFirst(); 
     if(persistedCase.getPatientInfo() != null) { 
      persistedCase.getPatientInfo().removeFromRealm(); 
      persistedCase.setPatientInfo(null); 
     } 

     persistedCase.setPatientInfo(new RealmPatientInfo(patientInfo)); 
     realm.copyToRealmOrUpdate(realmCase); 
     realm.commitTransaction(); 
     realm.close(); 

任何意見是非常感謝。

+0

您的'新的RealmPatientInfo(patientInfo)'不是由Realm管理的,必須爲您管理,以便將其設置爲關係的其他成員。 – EpicPandaForce

回答

1

如果你想更換PatientInfo對象在RealmCase同時確保舊的對象已正確刪除,你可以做到以下幾點:

Realm realm = null; 
try { 
    realm = Realm.getDefaultInstance(); 
    realm.executeTransaction(new Realm.Transaction() { 
     @Override 
     public void execute(Realm realm) { 
      RealmQuery<RealmCase> query = realm.where(RealmCase.class); 
      RealmCase persistedCase = query.findFirst(); 
      PatientInfo oldPatientInfo = persistedCase.getPatientInfo(); 
      if(oldPatientInfo != null) { 
       oldPatientInfo.removeFromRealm(); 
      } 

      // New Objects either have to be copied first or created using createObject 
      PatientInfo newPatientInfo = realm.copyToRealm(new RealmPatientInfo(patientInfo)); 
      persistedCase.setPatientInfo(newPatientInfo); 
     } 
    }); 
} finally { 
    if(realm != null) { 
     realm.close(); 
    } 
} 

現在你必須手動刪除舊PatientInfo ,而級聯刪除可以自動發生:https://github.com/realm/realm-java/issues/1104

另外,例如當使用list.add(item)時,RealmList支持將對象自動複製到Realm,像setPatientInfo這樣的設置屬性需要首先保持對象。這可能是我們應該重新審視的事情,因此行爲更加一致。這也意味着你的第一個代碼示例會起作用。

1

您還應該首先將您的RealmPatientInfo課程保存到領域以使其管理。

try { 
    RealmPatientInfo patientInfo = new RealmPatientInfo(patientInfo); 
    patientInfo = realm.copyToRealmOrUpdate(patientInfo); 
    persistedCase.setPatientInfo(patientInfo); 
    realm.copyToRealmOrUpdate(realmCase); 
    realm.commitTransaction(); 
} catch(Exception e) { 
    if(realm != null) { 
     try { 
      realm.cancelTransaction(); 
     } catch(Exception e) { 
      Log.e(TAG, "Failed to cancel transaction", e); 
     } 
    } 
} finally { 
    if(realm != null) { 
     realm.close(); 
    } 
}