2015-12-16 46 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(); 
    } 
}