In the table, you can say 100 items, which is the best approach to get the last 20 objects.
One of the ways I can think of is to load all the objects, change the array, create a new array and create a loop from the results to fill the new array 20 times and return it.
Something like the following:
public ArrayList<DataObject> getLastItems (int qty){ RealmResults<DataObject>results = realm.where(DataObject.class).findAll(); Collections.reverse(results); ArrayList<DataObject>arrayList = new ArrayList<>(); for (int i = 0; i == qty; i++){ arrayList.add(results.get(i)); } return arrayList; }
Is there a faster way to do this in android using realm.io?
Update
this is still how it is handled.
public ArrayList<DataObject> getLastItems (int qty){ RealmResults<DataObject>results = realm.where(DataObject.class).findAll(); ArrayList<DataObject> arrayList = new ArrayList<>(); for (int i = results.size(); i > Math.max(results.size() - 20, 0) ; i--) { arrayList.add(results.get(i-1)); } return arrayList; }
android realm
J. vassallo
source share