Firestore giving same data with startAfter

My query

         Firebase.firestore.document(userId!!)                 .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGE_GROUPS)                 .document(messageGroupId!!)                 .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGES)                 .orderBy("createdTime", Query.Direction.DESCENDING)                 .limit(Constants.PAGE_SIZE.toLong())                 .addSnapshotListener { querySnapshot, firebaseFirestoreException ->                    if (firebaseFirestoreException != null) {                         [email protected]                      }                  if (querySnapshot?.documents.isNotNullOrEmpty() && lastVisible == null) {                          lastVisible = querySnapshot?.documents?.get(querySnapshot.documents.size-1)                    }                } 

For second page I am using

      Firebase.firestore.document(userId!!)         .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGE_GROUPS)         .document(messageGroupId!!)         .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGES)         .orderBy("createdTime", Query.Direction.DESCENDING)         .startAfter(lastVisible)         .limit(Constants.PAGE_SIZE.toLong())         .addSnapshotListener { querySnapshot, firebaseFirestoreException ->          if (firebaseFirestoreException != null) {             [email protected]         }         if (querySnapshot?.documents.isNotNullOrEmpty() ) {                lastVisible = querySnapshot?.documents?.get(querySnapshot.documents.size - 1)         }      } 

Even after adding .startAfter(lastVisible) in second query the result is the same . My page size is 25 and I get same 25 elements with first and second query . Here createdTime is a Timestamp . The documentation says that this will give next 25 elements . So what am I doing wrong ?

Add Comment
1 Answer(s)

I fixed it by passing .startAfter(lastVisibleMessage?.createdTime) instead of DocumentSnapshot

Here are the changes I made

Firebase.firestore.document(userId!!)                 .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGE_GROUPS)                 .document(messageGroupId!!)                 .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGES)                 .orderBy("createdTime", Query.Direction.DESCENDING)                 .limit(Constants.PAGE_SIZE.toLong())                 .addSnapshotListener { querySnapshot, firebaseFirestoreException ->                    if (firebaseFirestoreException != null) {                         [email protected]                      }                  //parsing querySnapshot to my list of objects                 val messagesListTmp = getMessagesFromDocumentSnapshot(querySnapshot)                  if (querySnapshot?.documents.isNotNullOrEmpty() && lastVisibleMessage == null) {                         lastVisibleMessage = messagesListTmp.last()                    }                } 

Second Query:-

  Firebase.firestore.document(userId!!)     .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGE_GROUPS)     .document(messageGroupId!!)     .collection(Constants.FIREBASE_DB_COLLECTION_MESSAGES)     .orderBy("createdTime", Query.Direction.DESCENDING)     .startAfter(lastVisibleMessage?.createdTime)     .limit(Constants.PAGE_SIZE.toLong())     .addSnapshotListener { querySnapshot, firebaseFirestoreException ->      if (firebaseFirestoreException != null) {         [email protected]     }          //parsing querySnapshot to my list of objects       val messagesListTmp = getMessagesFromDocumentSnapshot(querySnapshot)         if (querySnapshot?.documents.isNotNullOrEmpty()) {                lastVisibleMessage = messagesListTmp.last()           }  } 
Answered on August 29, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.