Firebase 如何对我从cloud firestore获取的实时数据进行分页?
我正在构建一个聊天应用程序,并用下面的代码显示文本消息。我想知道如何对我从cloud firestore获取的实时数据进行分页,以便用户只能使用分页3.0库查看过去的消息,比如30条消息 聊天记录视图模型Firebase 如何对我从cloud firestore获取的实时数据进行分页?,firebase,kotlin,google-cloud-firestore,android-livedata,android-paging,Firebase,Kotlin,Google Cloud Firestore,Android Livedata,Android Paging,我正在构建一个聊天应用程序,并用下面的代码显示文本消息。我想知道如何对我从cloud firestore获取的实时数据进行分页,以便用户只能使用分页3.0库查看过去的消息,比如30条消息 聊天记录视图模型 fun listenForMessages(user: User): LiveData<List<String>> { val toId = user.uid val betweenList = mutableListOf(Id(from
fun listenForMessages(user: User): LiveData<List<String>> {
val toId = user.uid
val betweenList = mutableListOf(Id(fromId), Id(toId))
val sortedList = betweenList.sortedBy { it.Id }
repository.getChatReference()?.whereEqualTo("between", sortedList)
?.addSnapshotListener { documents, e ->
if (e != null) {
return@addSnapshotListener
}
if (documents != null) {
for (document in documents) {
docId = document.id
docids.clear()
docids.add(docId)
}
}
docList.value = docids
}
return docList
}
fun listener(docId: String): LiveData<List<ChatMessage>> {
repository.getChatReference()?.document(docId)?.collection("Messages")?.orderBy(
"timestamp",
Query.Direction.ASCENDING
)?.addSnapshotListener { snap, e ->
if (e != null) {
return@addSnapshotListener
}
if (snap != null) {
messages.clear()
for (doc in snap.documents) {
val chatMessage = doc.toObject(ChatMessage::class.java)
if (chatMessage != null) {
messages.add(chatMessage)
isScrollable.value = true
}
}
}
messageList.value = messages
}
return messageList
}
存储库
class ChatRepository @Inject constructor() {
fun getUserReference(uid:String): DocumentReference? {
return Firebase.firestore.collection("Users").document(uid)
}
fun getChatReference(): CollectionReference? {
return Firebase.firestore.collection("ChatChannels")
}
}
您是否看到了有关分页和限制数据的Firebase文档@FrankvanPuffelen是的,我想使用分页3.0库来实现。您是否已经尝试将文档中的解释和代码应用到该库中了?您共享的代码中似乎没有这样做,这使得堆栈溢出的问题有点宽泛。一般建议:对于将访问实时动态数据的聊天应用程序,
实时数据库
是最佳选择。它将解决您的人的使用情况,并节省大量的成本以及@完全正确。文档中只谈到改造网络呼叫和本地数据库,而没有提到在线数据库
class ChatRepository @Inject constructor() {
fun getUserReference(uid:String): DocumentReference? {
return Firebase.firestore.collection("Users").document(uid)
}
fun getChatReference(): CollectionReference? {
return Firebase.firestore.collection("ChatChannels")
}
}