我的查询
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个元素。这里createdTime
是Timestamp
。 documentation说,这将给出下25个元素。那我在做什么错了?
答案 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()
}
}