How can I set LIMIT in query in Realm?

后端 未结 4 2084
走了就别回头了
走了就别回头了 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<Item> 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<Item> resultsAll = realm.where(Item.class).findAll();
    final RealmResults<Item> resultsFilter = realm.where(Item.class)
                    .between("id", resultsAll.size() - LIMIT, resultsAll.size())
                    .findAllSorted("id", Sort.DESCENDING);
    
    0 讨论(0)
  • 2020-12-06 15:35

    There is no reason to use pagination with Realm if you use RealmResults<T> directly, because the elements in RealmResults are lazy evaluated, and aren't in memory until you call .get(i).

    Meaning, the query doesn't execute and evaluate an element until you directly index it. Which means, they aren't in memory. The RealmResults<T> list doesn't actually contain the elements, it just knows how to find them.

    As such, there is no LIMIT in Realm.

    Please note that if I remember correctly, reevaluating two RealmResults that are not returned by findAllSorted can have different ordering (if deletions occur). If the order must be the same no matter what, then consider a rank property, and order by findAllSorted("rank", Sort.ASCENDING).

    If you really want pagination, you should have the rank parameters, and then you can create a query like

    realm.where(SomeClass.class)
         .greaterThanOrEqualTo("rank", pageSize*pageIndex + 0)
         .lessThan(SomeClassFields.RANK, pageSize*pageIndex + pageSize)
         .findAllSorted(SomeClassFields.RANK, Sort.ASCENDING);
    

    Also, you should consider using RealmRecyclerViewAdapter instead from here:

    compile 'io.realm:android-adapters:1.3.0' // for Realm 0.89.0 to Realm 2.3.0
    

    or

    compile 'io.realm:android-adapters:2.1.0' // for Realm 3.0.0+    
    

    or

    compile 'io.realm:android-adapters:3.0.0' // for Realm 5.0.0+    
    

    The RealmRecyclerViewAdapter handles "loading the new data" for you, you don't have to do anything to make it work beyond setting the initial RealmResults.



    I'll actually have to change this answer once there is proper integration with the Paging Architecture Component, who knew? I have an experiment up with it here and you can see if it works for you.

    0 讨论(0)
  • 2020-12-06 15:40

    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

    0 讨论(0)
  • 2020-12-06 15:40

    Now limit is supporting

    I did that using limit method, You should use latest 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 讨论(0)
提交回复
热议问题