17

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.

Linh
  • 57,942
  • 23
  • 262
  • 279

3 Answers3

42

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.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 1
    Hey @EpicPandaForce can you explain why this is bad? I don't want my views to be aware of Realm as I'd like to swap out the underlying data store technology if needed. Basically my View/Presenters shouldn't be aware of Realm at all. Hence I simply want to return a List instead of a RealmResults – b.lyte Jul 11 '17 at 00:08
  • @clu The answer depends on whether you're listening to the database, or you think that requesting data only once from the db will be sufficient as no other thread will *ever* modify the objects inside the database (see [here](https://stackoverflow.com/questions/43192015/realm-working-with-clean-architecture-and-rxjava2/43202425#43202425)). But `realm.copyFromRealm()` creates mutable objects, if you want to replace RealmObjects as domain models and pay the price that is eager evaluation, mapping to immutable value objs is generally a better choice. – EpicPandaForce Jul 11 '17 at 06:50
  • @EpicPandaForce Yeah I see your point, but I'd rather have the view be unaware of this detail. I'd rather have the repository/datastore sending out updates to registered views. This way I can decide, if it makes more sense to use Room, for example, instead of Realm. Any something I'm still experimenting with. – b.lyte Jul 12 '17 at 05:50
  • With Realm, you should write your code in such a way that RealmResults should be easily replaced with LiveData. Not `List`, but `LiveData>`. – EpicPandaForce Jul 12 '17 at 07:14
  • @clu if you need to separate Realm in such a way that you don't need it as an implementation detail, then you should use PagedResults with a mapped data source using [Realm-Monarchy](https://github.com/Zhuinden/realm-monarchy/) (which I wrote to make this possible easily) – EpicPandaForce May 30 '18 at 12:42
  • @EpicPandaForce, can you look into this question? https://stackoverflow.com/questions/56052965/realm-returns-empty-list-of-object?noredirect=1#comment98746995_56052965 – Avi Patel May 09 '19 at 06:08
0

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);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Nicholas Ng
  • 1,428
  • 18
  • 23
  • 1
    Of course it's faster, the original question wanted the provided objects to not be `__RealmProxy`, which requires reading them from the Realm and copying them to unmanaged objects. – EpicPandaForce Aug 16 '17 at 12:33
0

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)
vlatkozelka
  • 909
  • 1
  • 12
  • 27
Gama_aide
  • 61
  • 4