错误在Realm中执行更新

时间:2015-12-16 01:00:15

标签: java android realm

我正在尝试更新我的RelamObject中的属性(在本例中为RealmCase)。我已经尝试了下面的代码,虽然我在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 = 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();

非常感谢任何建议。

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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

另外,例如RealmList支持在使用list.add(item)时自动将对象复制到Realm,设置setPatientInfo等属性要求首先保留对象。这可能是我们应该重新审视的内容,因此行为更加一致。这也意味着您的第一个代码示例将起作用。