I am developing an android app with post and comments using the firebase firestore database. In the comment section, I am loading the lastest 20 comments using the following code
val db= FirebaseFirestore.getInstance()
db.collection("comments")
.whereEqualTo("post_id", postId)
.limit(20)
.orderBy("time", Query.Direction.DESCENDING)
.get(Source.SERVER)
.addOnSuccessListener { documents ->
}
.addOnFailureListener { exception ->
}
I show these comments in recyclerview. When the user clicks previous comments I fetch timestamp of older comment and run following code
val db= FirebaseFirestore.getInstance()
db.collection("comments")
.limit(20)
.whereLessThan("time",timestamp)
.whereEqualTo("post_id", postId)
.orderBy("time", Query.Direction.DESCENDING)
.get(Source.SERVER)
.addOnSuccessListener { documents ->
}
.addOnFailureListener { exception ->
}
it fetches 20 documents from firestore database. But it doesn't fetch older comment. Instead, it fetches the latest comments.
when I remove .orderBy("time", Query.Direction.DESCENDING) it loads very first comments from document for that post id
For example.If the post contains 60 comments. first, I want to load 60-40 comments. then I want to load 40-20. then 20-0 .