Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在域中执行更新时出错_Java_Android_Realm - Fatal编程技术网

Java 在域中执行更新时出错

Java 在域中执行更新时出错,java,android,realm,Java,Android,Realm,我试图更新RelamObject中的一个属性(本例中为RealmCase)。我尝试了下面的代码,在我的领域中似乎没有任何更新。虽然我在RealmPatientInfo表中确实看到了一个新对象,但我的RealmCase中没有创建任何关系 RealmCase realmCase = new RealmCase(); realmCase.setId(getId()); realmCase.setPatientInfo(new RealmPatientInfo(patientIn

我试图更新RelamObject中的一个属性(本例中为RealmCase)。我尝试了下面的代码,在我的领域中似乎没有任何更新。虽然我在RealmPatientInfo表中确实看到了一个新对象,但我的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();
我也尝试了以下方法,但得到一个异常,表示该值不是由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();
Realm-Realm=Realm.getDefaultInstance();
realm.beginTransaction();
RealmQuery=realm.where(RealmCase.class);
RealmCase persistedCase=query.findFirst();
persistedCase.setPatientInfo(新的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();
Realm-Realm=Realm.getDefaultInstance();
realm.beginTransaction();
RealmQuery=realm.where(RealmCase.class);
RealmCase persistedCase=query.findFirst();
if(persistedCase.getPatientInfo()!=null){
persistedCase.getPatientInfo().removeFromRealm();
persistedCase.setPatientInfo(空);
}
persistedCase.setPatientInfo(新的RealmPatientInfo(patientInfo));
realm.copyToRealmOrUpdate(realmCase);
realm.commitTransaction();
realm.close();

非常感谢您的建议。

您还应该先将您的
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();
    }
}

如果要在确保正确删除旧对象的同时替换
RealmCase
中的
PatientInfo
对象,可以执行以下操作:

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();
    }
}
Realm=null;
试一试{
realm=realm.getDefaultInstance();
realm.executeTransaction(新realm.Transaction(){
@凌驾
public void execute(领域领域){
RealmQuery=realm.where(RealmCase.class);
RealmCase persistedCase=query.findFirst();
PatientInfo-oldPatientInfo=persistedCase.getPatientInfo();
如果(oldPatientInfo!=null){
oldPatientInfo.removeFromRealm();
}
//必须首先复制新对象,或者使用createObject创建新对象
PatientInfo newPatientInfo=realm.copyToRealm(新RealmPatientInfo(PatientInfo));
persistedCase.setPatientInfo(newPatientInfo);
}
});
}最后{
if(realm!=null){
realm.close();
}
}
现在,您必须手动删除旧的
PatientInfo
,而级联删除可以自动执行:


此外,例如,RealmList支持在使用
列表时自动将对象复制到领域。添加(项)
,设置
setPatientInfo
等属性要求首先保存对象。这可能是我们应该重新考虑的问题,以便行为更加一致。这也意味着您的第一个代码示例可以工作

您的
新RealmPatientInfo(patientInfo)
不是由Realm管理的,您必须对其进行管理才能将其设置为关系的其他成员。