Limit Realm Results

How to limit the number of objects returned by Realm? .FindAll returns all the rows matching the request, and .findFirst returns only the first. But what about the first 1000? .FindAll can return so many rows that it consumes too much memory.

+8
android realm
source share
5 answers

The most interesting thing is that you do not need to worry about it in Realm. The result object returned from the query lazily loads the objects and their fields when accessing them. Your objects are never copied and therefore are displayed only once in memory / disk.

Detailing (current) implementation of this object is that the RealmResults object returned from the request is a list of links to the corresponding objects. These links are tiny numbers that are kept compressed, so they take up very little memory. So even with 100,000 matches, in fact, it does not take up much memory. And it will take about the same amount of memory for all types of objects, whether it be one int field or hundreds of fields with strings or large binary files.

+14
source share

I figured out a solution to achieve this after so many days using the โ€œbetweenโ€ requests, as shown in the official docs https://realm.io/docs/java/latest

If you want to get the first N elements, just pass the score with and with the field name below

realm.where(clazz).between("count",0,1000).findAll(); 

Where

"count" = Field name (for example, the variable name in your pojo)
"0" = Range from
"1000" = Range to

For example: the above query will retrieve the first 0 to 1000 as RealmResults.

Note. The above solution only works if you have a unique identifier with the number of rows. In my case, I manually inserted the row counter value before pasting the values โ€‹โ€‹into Realm.

+1
source share

One way could be this if you really want only RealmResults using the Stream limit method:

 //get all results of your choice query RealmResults<TypeClass> entities = realm.where(TypeClass.class).findAll(); //limit using Stream List<Integer> ids = Stream.of(entities).limit(10).map(x -> x.Id).collect(Collectors.toList()); //return only those ids elments return realm.where(TypeClass.class).in("Id", ids.toArray(new Integer[])).findAll(); 
0
source share

Realm does not currently provide any restriction function, but if you want the first N elements

 int N=10; // whatever value you want Realm mRealm=Realm.getDefaultInstance(); RealmResults<Example> list= mRealm.where(Example.class).findAll(); list.subList(0,N); 

for the last N elements

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

I did this using the limit method. You should use the latest classpath "io.realm:realm-gradle-plugin:5.8.0" version classpath "io.realm:realm-gradle-plugin:5.8.0"

 RealmResults<YourPOJOClass> realmResults = mRealm.where(YourPOJOClass.class).sort("createdTime").limit(10).findAll(); //here this record will be sorted by ascending order using schema name "createdTime" //this will return the 10 rows only. 

''

0
source share

All Articles