Skip to content Skip to sidebar Skip to footer

How To Filter Data Based On Items Count In Firebase Database

I am writing an android chat app and I am trying to implement endless scrolling using Firebase Database in RecyrclerView using custom FirebaceRecyclerAdapter. For first messages lo

Solution 1:

I'm pretty sure I answered this less than a day ago, so I recommend scanning my answers. But I'll repeat.

The Firebase Database API is inherently not very well suited for pagination. Think of what happens when an item is inserted while the user is on page 1 (with the first ten items). They end up seeing the previous item 10 at the start of page 2, but will never have seen the newly inserted item.

That said, if you want to do pagination, it is technically possible. But instead of wanting to skip 10 items, you will instead have to tell Firebase to start the query at the last item from the previous page. This is called an anchor item and in code it looks like this:

FirebaseDatabase
        .getInstance()
        .getReference()
        .child(RequestParameters.FB_CHILD_MESSAGES)
        .orderByChild(RequestParameters.FB_CHILD_MESSAGES_ORDERING_TIME)
        .startAt(timeOfLastItemOnPreviousPage, keyOfLastItemOnPreviousPage)
        .addListenerForSingleValueEvent(...)

The key properties here are:

  • timeOfLastItemOnPreviousPage: the value of the FB_CHILD_MESSAGES_ORDERING_TIME property of the last item on the previous page

  • keyOfLastItemOnPreviousPage, the key of the last item on the previous page (which is needed in case there are multiple items with the same value for FB_CHILD_MESSAGES_ORDERING_TIME

Post a Comment for "How To Filter Data Based On Items Count In Firebase Database"