Get the data from the Firebase in limit to perform pull to refresh and load more functionality

前端 未结 2 1995
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 09:54

Yet now i am getting the all data from the FireBase at one time.What i want to do that getting data in LIMITS like 15 records at a tim

2条回答
  •  盖世英雄少女心
    2020-12-01 10:36

    You are missing orderByKey(). For any filtering queries you must use the ordering functions. Refer to the documentation

    In your onRefresh method you need to set the limit:

     public void onRefresh() {
         // Refresh items  
         ///HERE "oldestPostId" IS THE KEY WHICH I GET THE LAST RECORDS FROM THE FIREBASE
                    mChatRef.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
    .....
    

    So the data you retrieve is the only 10 new records after you got your first 10 records.

    Make sure to save the oldest key of the newly retrieved data so that on next refresh new data from this key is only retrieved.

    Suggestion: Instead of adding a child value listener to find the last key, you can just use the value listener and get the last data snapshot with the size to get the last record's key.

提交回复
热议问题