如何转换RealmResults< Object>列出< Object> [英] How to convert RealmResults<Object> to List<Object>

查看:67
本文介绍了如何转换RealmResults< Object>列出< Object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从Realm收到的RealmResults,例如

I have RealmResults that I receive from Realm like

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

现在我想将RealmResults<StepEntry>转换为ArrayList<StepEntry>

我尝试过

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

,但是我的ArrayList中的项目不是我的StepEntry对象,而是StepEntryRealmProxy

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

如何转换? 任何帮助或建议将不胜感激.

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

推荐答案

要热切地从Realm中读取每个元素(因此可以使列表中的所有元素都变得不受管理):

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

但是通常来说,除非您想使用GSON序列化对象(特别是因为它使用反射而不是使用getter读取字段数据)来进行序列化,否则绝对没有理由,因为Realm的设计方式是使列表公开更改侦听器,通过观察数据库所做的更改,您可以使UI保持最新.

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.

这篇关于如何转换RealmResults&lt; Object&gt;列出&lt; Object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆