0

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

And this is how the firestore collection looks like enter image description here

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
NOOB1223
  • 45
  • 1
  • 4
  • What are the values of "startAt"? Please provide a more detailed screenshot of your database that includes ".collection("${collectionPath}/$postId/comments")". – Alex Mamo Apr 12 '21 at 08:24
  • @AlexMamo values of "startAt" is a timestamp.I used it for pagination – NOOB1223 Apr 12 '21 at 08:29
  • What value does the function receive as StartAt?. I have the feeling that the problem could be from there. Could you provide and example of the function in general? Maybe some of the data given isn't valid when ordering descending. – Anton L Jun 02 '21 at 08:14

0 Answers0