Realm.io Android is the best approach to get the last 20 items from a table

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; } 
+7
android realm
source share
5 answers

Also note that Realm tables are disordered. Think of them as a bag in which you put your data. This means that if you want the last 20 elements to be inserted, you will need to add a field containing the insert time. It will also allow you to achieve the desired result:

 RealmResults<DataObject>results = realm.where(DataObject.class) .findAllSorted("timestamp", RealmResults.SORT_ORDER_DESCENDING); for (int i = 0; i < 20; i++) { // do magic here } 
+7
source share

This question was almost closed, but I found another way to deal with this situation, I don’t know if the best option or good practice, @ChristianMelchior can say better than me about it (I saw your collaboration with the realm project) ... well, codes say more than words.

 RealmResults<Appointment> appointments; List<Appointment> appointments = appointments.subList(0, appointments.size()); 

As we can see, answering your question Johan, you can simply change the index to get all the objects you want!

+6
source share

RealmResults only creates the objects that you are actually using, so iterating over all of them to modify the list will be very bad. Instead, as described by bmunk, you should simply find the correct index and start with it, as shown below. Note that Realms are unordered, so the items returned will not be correctly detected without sorting.

 public ArrayList<DataObject> getLastItems (int qty){ RealmResults<DataObject>results = realm.where(DataObject.class).findAll().sort("fieldName"); ArrayList<DataObject> arrayList = new ArrayList<>(); for (int i = Math.max(result.size() - 20, 0); i < results.size() ; i++) { arrayList.add(results.get(i)); } return arrayList; } 
+2
source share

You should try to avoid copying objects unless strictly required. After you have done findAll (), you already have an ordered list. Then you can simply use max (results.size () - 20, 0) to find out which index starts your iteration instead of copying.

0
source share

Do it like

 Realm mRealm=Realm.getDefaultInstance(); RealmResults<Example> list= mRealm.where(Example.class).findAll(); list.subList(list.size()-20,list.size()); 
0
source share

All Articles