我有从 Realm
收到的 RealmResults喜欢
RealmResults<StepEntry> stepEntryResults = realm.where(StepEntry.class).findAll();
现在我要转换 RealmResults<StepEntry>
至 ArrayList<StepEntry>
我试过了
ArrayList<StepEntry> stepEntryArray = new ArrayList<StepEntry>(stepEntryResults));
但是我的 ArrayList
中的项目不是我的StepEntry
对象,它是StepEntryRealmProxy
如何转换? 任何帮助或建议将不胜感激。
最佳答案
要急切地读取 Realm 中的每个元素(并因此使列表中的所有元素都变成非托管的,您可以这样做):
List<StepEntry> arrayListOfUnmanagedObjects = realm.copyFromRealm(realmResults);
但是你通常绝对没有理由这样做,除非你想用 GSON 序列化对象(具体来说,因为它用反射而不是用 getter 读取字段数据),因为 Realm 的设计方式是列表公开一个更改监听器,允许您仅通过观察对数据库所做的更改来使您的 UI 保持最新。
关于android - 如何将 RealmResults<Object> 转换为 List<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758854/