简体   繁体   中英

Error Performing an update in Realm

I am trying to update a property inside my RelamObject(RealmCase in this case). I have tried the code below and it looks like nothing is updated in my Realm although I do see a new object in the RealmPatientInfo Table, there is just no relationship created from my RealmCase.

    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();

I have also tried the following but get an exception saying the value is not managed by 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();

I also want to remove the old patientInfo object(both the reference and the entry in the RealmPatientInfo table) Below is my attempt at that, although my error from before has prevented me from testing that part.

        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();

Any advice is greatly appreciated.

You should also save your RealmPatientInfo class first to the realm as well to make it managed.

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();
    }
}

If you want to replace the PatientInfo object in a RealmCase while making sure the old object is properly deleted you can do the following:

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();
    }
}

Right now you have to manually delete the old PatientInfo , with cascading deletes that can happen automatically instead: https://github.com/realm/realm-java/issues/1104

Also, eg RealmList supports copying objects automatically to Realm when using list.add(item) , setting properties like setPatientInfo require the object to be persisted first. This is probably something we should revisit so the behaviour is more consistent. It would also have meant that your first code sample would have worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM