android - Firestore Android Orderby Descending 不工作
问题描述
所以,我有一个 firestore 集合,我想按降序获取数据,但它不起作用。结果总是给出一个空列表。但不知何故,如果我将排序方向更改为升序,查询就可以完美运行。这是他的代码:
fun getComments(uid: String, category: String, postId: String, limit: Long , startAt : Timestamp?, listener: GenericGetListener<List<CommentModel>>) {
val collectionPath = getCollectionPath(category)
if (collectionPath == null) {
listener.onError(Exception("Not valid"))
return
}
val commentListRef = db.collection("${collectionPath}/$postId/comments")
commentListRef
.orderBy("timestamp", Query.Direction.DESCENDING)
.limit(limit)
.startAt(startAt)
.get()
.addOnSuccessListener { querySnapshot ->
val commentList = querySnapshot.toObjects(CommentModel::class.java)
db.runTransaction { transaction ->
return@runTransaction commentList.map { commentModel ->
val userUid = commentModel.uid
if(userUid != null){
val user = transaction.get(
db.collection(CollectionsPath.users).document(userUid)
).toObject(UserProfileModel::class.java)
if(user != null){
commentModel.apply {
this.profilePic = user.profilePic
this.name = user.name
this.verifiedStatus = user.verifiedStatus
this.accCategory = user.accType
this.subAccType = user.subAccType
this.category = user.category
}
}
}
commentModel
}
}.addOnSuccessListener {
listener.onSuccess(it)
}.addOnFailureListener {
listener.onError(it)
}
}
.addOnFailureListener {
listener.onError(it)
}
}
firestore 集合是一个嵌套集合,具有这种结构 post->postId->comment
解决方案
推荐阅读
- python - 如何使用 Pandas 将第二天日期低于现有日期值
- node.js - 如何在终端node.js中显示有错误的整行
- python - 通过带有 if 语句的函数对数据帧中的数字执行操作
- linux - 在目录中使用文件路径/文件名在两个模式之间打印
- java - 用于 CRUD 操作的 REST api 的演示 Spring Boot 应用程序抛出 404 not found 错误
- python - NameError:名称“sklearn”未定义
- python - 如何将xml文件拖放到Qtableview中并同时解析xml
- python - 在添加到购物车之前存储产品的额外信息
- python - 使用python在文本文件中使用相应的标签元素访问和打印特定标签
- excel - 宏调用将针对工作簿中所有打开的工作表运行的函数