How to convert RealmResults <Object> to a list of <Object>

I have RealmResults that I get from Realm , for example

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

Now I want to convert RealmResults<StepEntry> to ArrayList<StepEntry>

I tried

  ArrayList<StepEntry> stepEntryArray = new ArrayList<StepEntry>(stepEntryResults)); 

but the element in my ArrayList not my StepEntry object, it is StepEntryRealmProxy enter image description here

How can I convert it? Any help or suggestion would be greatly appreciated.

+11
source share
3 answers

To read each item from the Kingdom with impatience (and therefore make all items in the list unmanageable, you can do this):

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

But you, as a rule, have absolutely no reason to do this, unless you want to serialize objects with GSON (in particular, because it reads these fields with reflection, and not with the recipients), because Realm was designed in such a way that the list provides a change listener, allowing you to keep your interface up to date by simply watching the changes made to the database.

+32
source

The answer from @EpicPandaForce works well. I tried in this way to optimize the performance of my application, and I found that 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
source

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) 
-1
source

Source: https://habr.com/ru/post/1260192/


All Articles