Limit Realm results

前端 未结 4 551
情深已故
情深已故 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条回答
  •  萌比男神i
    2020-12-16 20:25

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

    //get all results of your choice query
    RealmResults entities = realm.where(TypeClass.class).findAll();
    //limit using Stream
    List 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();
    

提交回复
热议问题