在相关表中添加新条目后,在Android RealmDB中自动删除行?

时间:2017-01-25 23:55:18

标签: android realm realm-list

背景: isolateLocation和Susceptibilities(1-many)具有一对多的关系以及药物和易感性(1-many)。所有都是RealmObjects和IsolateLocation以及Drug包含RealmList of Susceptibilities。在每个isolateLocation中为所有药物创建易感性,然后将此易感性添加到相应的IsolateLocation易感性列表和药物易感性列表中。

问题:第一个易感性对象被正确添加到两个模型中的列表中(Drug& IsolateLocation),但创建的下一个易感性在添加后立即正确添加到药物模型中在药物易感性列表中,删除了IsolateLocation易感性列表中的上一个条目。因此,最终药物敏感性与多种易感性正确,但IsolateLocation易感性列表仅有1。

原始数据: http://imgur.com/a/iTPY8

代码:

for (Drug drug : drugList){
    Susceptibility susceptibility = new Susceptibility();
    susceptibility.setId(UUID.randomUUID().toString());
    susceptibility.setDrug(drug);
    susceptibility.setReference(parsedCsv[drug.getId()+1]);
    susceptibility.setSusceptibilityValue(parsedCsv[drug.getId()]);
    susceptibility.setIsolateLocation(isolateLocation);
    addSusceptibilityToRealm(isolateLocation, drug, susceptibility);
    }


private void addSusceptibilityToRealm(IsolateLocation isolateLocation, Drug drug, Susceptibility susceptibility) {
    realm.beginTransaction();
    Drug drugEntry = realm.where(Drug.class).equalTo("id", drug.getId()).findFirst();
    drugEntry.getSusceptibilities().add(susceptibility);

    IsolateLocation isolateLocationEntry = realm.where(IsolateLocation.class).equalTo("id", isolateLocation.getId()).findFirst();
    isolateLocationEntry.getSusceptibilities().add(susceptibility);
    realm.commitTransaction();
}

3 个答案:

答案 0 :(得分:0)

您需要将realm managed Susceptibility个实例插入drugEntry.getSusceptibilities()isolateLocationEntry.getSusceptibilities()。即,您需要按方法Susceptibility创建新的realm.createObject()实例:

for (Drug drug : drugList){
    String id = UUID.randomUUID().toString();
    realm.beginTransaction();
    // Create a new object
    Susceptibility susceptibility = realm.createObject(Susceptibility.class, id);
    /*
    setup properties of susceptibility instances
    ....
    */
    addSusceptibilityToRealm(isolateLocation, drug, susceptibility);
    realm.commitTransaction();
}

或者,在将其插入realm.copyToRealm()drugEntry.getSusceptibilities()之前,您需要通过调用isolateLocationEntry.getSusceptibilities()非托管实例插入领域:

private void addSusceptibilityToRealm(IsolateLocation isolateLocation, Drug drug, Susceptibility susceptibility) {
    realm.beginTransaction();
    // Copy the object to Realm. Any further changes must happen on susceptibility
    Susceptibility susceptibility = realm.copyToRealm(susceptibility);

    Drug drugEntry = realm.where(Drug.class).equalTo("id", drug.getId()).findFirst();
    drugEntry.getSusceptibilities().add(susceptibility);

    IsolateLocation isolateLocationEntry = realm.where(IsolateLocation.class).equalTo("id", isolateLocation.getId()).findFirst();
    isolateLocationEntry.getSusceptibilities().add(susceptibility);
    realm.commitTransaction();
}

有关托管对象创建的更多信息,您可以阅读official documentationthis answer

答案 1 :(得分:0)

那是因为你应该像这样插入

realm.beginTransaction();
for (Drug drug : drugList){
    Susceptibility susceptibility = realm.createObject(Susceptibility.class, UUID.randomUUID().toString());
    Drug drugEntry = realm.where(Drug.class).equalTo("id", drug.getId()).findFirst();
    susceptibility.setDrug(drugEntry);
    susceptibility.setReference(parsedCsv[drug.getId()+1]);
    susceptibility.setSusceptibilityValue(parsedCsv[drug.getId()]);
    susceptibility.setIsolateLocation(isolateLocation);
    drugEntry.getSusceptibilities().add(susceptibility);
    IsolateLocation isolateLocationEntry = realm.where(IsolateLocation.class).equalTo("id", isolateLocation.getId()).findFirst();
    isolateLocationEntry.getSusceptibilities().add(susceptibility);
    //addSusceptibilityToRealm(isolateLocation, drug, susceptibility);
}
realm.commitTransaction();

答案 2 :(得分:0)

我在发布上述答案之前找到了一个解决方案,不确定它是否是正确的方法。

在将所有数据解析为内存中List<IsolateLocation> isolateLocationListList<Drug> drugList之前,我没有保留任何数据,然后没有为Susceptibility创建域管理对象。已将List<Susceptibility> susceptibilitiesList添加到drugListisolateLocationList,并将此方法称为

private void addDataToRealm() {
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.copyToRealm(isolateLocationList);
            realm.copyToRealmOrUpdate(drugList);
        }
    });
}

第二行是copyToRealmOrUpdate,因为一些药物在执行第一行后被保存到领域,这是由于模型之间的关系,但第二行有关于药物的所有完整数据,所以,如果已存在的条目更新它。