2016-09-18 2 views
0

"Encounter"라는 RealmObject가 있는데 여기에는 "SavedCombatant"라는 다른 RealmObjects의 RealmList가 포함되어 있습니다. 필자의 코드에서는 RealmList를 적절한 객체로 채 웁니다. 그러나 트랜잭션을 커밋하고 나중에 Encounter-Object를 검색 할 때 RealmList는 비어 있습니다.copyToRealm은 채워진 목록 대신 빈 목록을 복사합니다.

나는 이것이 나의 만남 클래스 될 것

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

다음 코드를 가지고

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 
} 

RealmList에 사용되는 클래스는 내가 RealmObject를 사용하고 있는지 확인하기 위해 다음과 같은 헤더 (이 여기도 마찬가지)

public class SavedCombatant extends RealmObject 
+0

음을 사용하여이 시도가 보여주는 내부 영역에 내 SavedCombatant 개체를 복사하는 데 필요한' – EpicPandaForce

답변

1

사실, Re 객체 안에 명시 적으로 객체를 저장해야합니다. almList.

내가 내 루프,

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

는 copyToRealm 또 다른 방법은 (목록 ) 거기에 setSavedCombatants'? –

+0

'for (E item : List items) copyToRealm (item); '아마도? – TormundThunderfist