Phan Van Linh Phan Van Linh - 17 days ago 5
Android Question

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

I have RealmResults that I receive from

Realm
like

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


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


I have try

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


but the item in my
ArrayList
is not my
StepEntry
object, it is
StepEntryRealmProxy

enter image description here

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

Answer

The answer is

 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).


Typically mixing realm.copyFromRealm anywhere else for any other reason shows a fundamental misunderstanding of what Realm was designed for. It's a zero-copy database, after all. Copying from a zero-copy database is iffy.