How to convert RealmResults<Object> to List<Object>

前端 未结 3 1352
轻奢々
轻奢々 2021-01-04 02:54

I have RealmResults that I receive from Realm like

RealmResults stepEntryResults = realm.where(StepEntry.class).findAll();


        
相关标签:
3条回答
  • 2021-01-04 03:29

    The answer by @EpicPandaForce works well. I tried this way to optimize my app performance and I find the following is a bit faster. Another option for people who prefer speed:

    RealmResults<Tag> childList = realm.where(Tag.class).equalTo("parentID", id).findAll();
    Tag[] childs = new Tag[childList.size()];
    childList.toArray(childs);
    return Arrays.asList(childs);
    
    0 讨论(0)
  • 2021-01-04 03:33

    To eagerly read every element from the Realm (and therefore make all elements in the list become unmanaged, you can do):

     List<StepEntry> arrayListOfUnmanagedObjects = realm.copyFromRealm(realmResults);
    

    But you generally have absolutely no reason to do that unless you want to serialize the objects with GSON (specifically, because it reads field data with reflection rather than with getters), because Realm was designed in such a way that the list exposes a change listener, allowing you to keep your UI up to date just by observing changes made to the database.

    0 讨论(0)
  • 2021-01-04 03:35

    In Kotlin:

    var list : List<Student>: listof()
    val rl = realm.where(Student::class.java).findAll()
    
    // subList return all data contain on RealmResults
    list = rl.subList(0,rl.size)
    
    0 讨论(0)
提交回复
热议问题