When I use realm.where(Model.class) it returns RealmResults and list item's fields are empty. How to convert queryset to readable ArrayList or iterate over RealmResults to get actual data from objects in DB?
All fetches are lazy in Realm, and the data is never copied. So if you want to get current data from RealmResults, you must call realm.copyFromRealm(results).
public List<Model> getModelList() {
List<Model> list = new ArrayList<>();
Realm realm;
try {
realm = Realm.getDefaultInstance();
RealmResults<Model> results = realm
.where(Model.class)
.findAll();
list.addAll(realm.copyFromRealm(results));
} finally {
if (realm != null) {
realm.close();
}
}
return list;
}
RealmResults<E> implements the Iterable<E> interface, so you can iterate with:
for (E e : realmResults) {
// Do stuff
}
If this does not answer the question, then please precise it and add a sample of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With