1

I would like to know how can I paginate with the new FirebaseFirestore database, when I use real time updates with the addSnapshotListener.

The problem I am having is that I can not update the limit() once I set the query, so it remains with the same limit, and I am not able to load more data.

This is what I currently have:

private Query query = db.collection("groups").orderBy("id", Query.Direction.DESCENDING).limit(15);

Those are the query parameters, and I have a ListenerRegistration to remove the database listener (I know it can be also doing passing the activity to the addSnapshotListener method).

This is the code to fetch the data:

registration = query.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        mGroupArrayList.clear();

        if (e != null) {
            Log.w("MAINACTY", "listen:error", e);
            return;
        }

        for (DocumentSnapshot dc : documentSnapshots.getDocuments()) {
            // Get the last visible document
            lastVisible = documentSnapshots.getDocuments()
                    .get(documentSnapshots.size() - 1);

            mGroupArrayList.add(dc.toObject(Group.class));
        }

        adapter.notifyDataSetChanged();
    }
});

As is said, the problem is that I can not update the limit I first set on the query, and if I try to do a new query, the old information disappears when an edit is made on the information or when I leave the activity.

In the documentation there is only an example with the get() method to paginate, but with that method there is not real time updates.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Jonny
  • 328
  • 3
  • 14

0 Answers0