Search code examples
androidrealmrealm-list

How can I set LIMIT in query in Realm?


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 duplicate value in it.

Here is what I attempted for pagination.

 RealmResults<Person> mPersonData=RealmUtils.getAllPersonWithTagsDescending(); 
    if (mPersonData != null) { 
    int startPos=getAllPerson.size()-1; 
    int endPos=mPersonData.size()-1; 
    List<Person> newPersonData=mPersonData.subList(startPos,endPos);   
    getAllPerson.addAll(newPersonData); 
    mAdapter.notifyDataSetChanged(); 
}

What am I doing wrong?


Solution

  • You can use limit from Realm 5.6.0+. It looks like this.

    val myDataList = Realm.getDefaultInstance()
        .where(MyData::class.java)
        .limit(10)
        .findAll()
    

    Look this document