Search code examples
androidrealmrealm-list

copyToRealm copies an empty list instead of a populated one


I have a RealmObject called "Encounter", which contains a RealmList of other RealmObjects called "SavedCombatant". In my code, I populate the RealmList with the appropriate objects, but when I commit the transaction and retrieve the Encounter-Object later, the RealmList is empty.

I have the following code

    public void saveEncounter(){

        //create a new key for the encounter
        int key = 0;
        if(mRealm.where(Encounter.class).count() != 0) {
            RealmResults<Encounter> encounters = mRealm.where(Encounter.class).findAll();
            Encounter encounter = encounters.last();
            key = encounter != null ? encounter.getKey() + 1 : 0;
        }

        // retrieve the data to populate the realmlist with
        // combatants has 1 element
        List<SavedCombatant> combatants = mAdapter.getCombatants();
        mRealm.beginTransaction();
        Encounter e = mRealm.createObject(Encounter.class);
        e.setKey(key);
        e.setTitle(txtTitle.getText().toString());
        RealmList<SavedCombatant> combatantRealmList = new RealmList<>();
        for (int i = 0; i < combatants.size(); i++) {
            combatantRealmList.add(combatants.get(i));    
        }
        //combatantRealmList also has 1 element. setCombatants is a
        //generated Setter with a couple bits of additional logic in it
        e.setCombatants(combatantRealmList);
        mRealm.copyToRealm(e);
        mRealm.commitTransaction();
}

this would be my Encounter class

    public class Encounter extends RealmObject {

    private int key;
    private String title;
    private RealmList<SavedCombatant> combatants;

    @Ignore
    private String contents;

    public void setCombatants(RealmList<SavedCombatant> combatants) {
        //simple setter
        this.combatants = combatants;

        //generate summary of the elements in my realmlist. (probably inefficient as hell, but that's not part of the problem)
        HashMap<String, Integer> countMap = new HashMap<>();
        for (int i = 0; i < combatants.size(); ++i) {
            String name = combatants.get(i).getName();
            int countUp = 1;
            if (countMap.containsKey(name)) {
                countUp = countMap.get(name) + 1;
                countMap.remove(name);
            }
            countMap.put(name, countUp);
        }
        contents = "";
        Object[] keys = countMap.keySet().toArray();
        for (int i = 0; i < keys.length; ++i) {
            contents += countMap.get(keys[i]) + "x " + keys[i];
            if (i + 1 < keys.length)
                contents += "\r\n";
        }
    }

    // here be more code, just a bunch of getters/setters
}

the class used for the RealmList has the following header (as to verify that I'm using a RealmObject here aswell)

public class SavedCombatant extends RealmObject

Solution

  • As it turns out, you need to explicitly save Objects inside a RealmList.

    I needed to copy my SavedCombatant object to realm inside my for loop, using

    mRealm.copyToRealm(combatants.get(i));