如何转换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.