How can I set LIMIT in query in Realm?

后端 未结 4 2099
走了就别回头了
走了就别回头了 2020-12-06 14:53

I have done R&D for limit in query with no success. There is one way with which to paginate data in Realm with sub list but no success with that. It shows d

4条回答
  •  感情败类
    2020-12-06 15:23

    As @EpicPandaForce said, you can use lessThan()and greaterThan() to set the limit, but realm have between() method, check: https://realm.io/docs/java/latest/#chaining-queries

    Example :

    Just get items with limit 50 :

    static int LIMIT = 50;
    final RealmResults resultsFilter = realm.where(Item.class)
                    .between("id", 0, LIMIT)
                    .findAllSorted("id", Sort.ASCENDING);
    

    Or when I want to get last 10 items:

    static int LIMIT = 10;
    final RealmResults resultsAll = realm.where(Item.class).findAll();
    final RealmResults resultsFilter = realm.where(Item.class)
                    .between("id", resultsAll.size() - LIMIT, resultsAll.size())
                    .findAllSorted("id", Sort.DESCENDING);
    

提交回复
热议问题