Firestore使用startAfter

时间:2020-08-25 10:42:37

标签: android firebase google-cloud-firestore

我的查询

         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) {
                        return@addSnapshotListener
                     }
                 if (querySnapshot?.documents.isNotNullOrEmpty() && lastVisible == null) {
                         lastVisible = querySnapshot?.documents?.get(querySnapshot.documents.size-1)
                   }
               }

我正在使用第二页

      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) {
            return@addSnapshotListener
        }
        if (querySnapshot?.documents.isNotNullOrEmpty() ) {
               lastVisible = querySnapshot?.documents?.get(querySnapshot.documents.size - 1)
        }

    }

即使在第二个查询中添加.startAfter(lastVisible)之后,结果也是相同的。我的页面大小是25,在第一个查询和第二个查询中,我得到相同的25个元素。这里createdTimeTimestampdocumentation说,这将给出下25个元素。那我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我通过传递.startAfter(lastVisibleMessage?.createdTime)而不是DocumentSnapshot

来解决它

这是我所做的更改

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) {
                        return@addSnapshotListener
                     }
                 //parsing querySnapshot to my list of objects
                val messagesListTmp = getMessagesFromDocumentSnapshot(querySnapshot)
                 if (querySnapshot?.documents.isNotNullOrEmpty() && lastVisibleMessage == null) {
                        lastVisibleMessage = messagesListTmp.last()
                   }
               }

第二个查询:-

  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) {
        return@addSnapshotListener
    } 
        //parsing querySnapshot to my list of objects
      val messagesListTmp = getMessagesFromDocumentSnapshot(querySnapshot)
        if (querySnapshot?.documents.isNotNullOrEmpty()) {
               lastVisibleMessage = messagesListTmp.last()
          }

}