So, I have a firestore collection, and I want to get the data with sort by descending, and It is not working. The result is always giving an empty list. But somehow if I change the sort orientation to ascending the query is working perfectly. Here is he code :
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)
}
}
The firestore collection is a nested collection with such structure post->postId->comment