Limit Realm results

前端 未结 4 547
情深已故
情深已故 2020-12-16 19:49

How do I limit the amount of objects Realm returns? .findAll returns all rows matching the query and .findFirst returns only the first. But what about something like first 1

4条回答
  •  無奈伤痛
    2020-12-16 20:14

    I figured out the solution to achieve this after so many days using "between" query as found in the official docs https://realm.io/docs/java/latest

    If you want to fetch first N elements, Simply pass the count from and to with field name as below

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

    Where,

    "count" = Field name (i.e variable name in your pojo)
    "0" = Range From
    "1000" = Range to

    For example: The above query will fetch first 0 to 1000 as RealmResults.

    Note: The above solution works only if you have some unique id with row count. In my case I manually inserted row count value before inserting the values into Realm.

提交回复
热议问题