I was looking into Firebase Firestore and what better sample project to create than another chat app.
How I imagine it would work:
- Newest messages are fetched when the user enters a fragment
- update
oldestTimestamp
, add messages to recycler view - when user scrolls to top
fecthMore
and get 20 messages that are older than the current oldest one update
oldestTimestamp
, add messages to recycler viewAfter reading order-limit-data and query-cursors I came up with those solutions, sadly none of them worked.
I used whereLessThanOrEqualTo("timestamp", previousOldestMessageTimestamp)
but this returned 0 messages
After that, I tried using .orderBy("timestamp", Query.Direction.DESCENDING) .endBefore(previousOldestMessageTimestamp)
same result as before
Then I tried simplifying the query to just .orderBy("timestamp") .endBefore(previousOldestMessageTimestamp)
which returned some messages but in the list were oldest messages in whole conversation
What should I change to make it work properly (as every chat app)?
Also can you explain difference between whereLessThanOrEqualTo()
and orderBy()
+ endBefore()
Here is some context
private var previousOldestMessageTimestamp: Long = -1
private fun firstFetch(){
//get oldest messages
//update previousOldestMessageTimestamp to oldest message timestamp
}
private fun fetchMore() {
val conversationRef = FirebaseFirestore.getInstance()
.document("conversations")
.collection("some_id")
conversationRef
.whereLessThan("timestamp", previousOldestMessageTimestamp)
//^ 0 messages
.orderBy("timestamp", Query.Direction.DESCENDING)
.endBefore(previousOldestMessageTimestamp)
//^ returns 0 messages
.orderBy("timestamp")
.endBefore(previousOldestMessageTimestamp)
//^ returns messages but starts with oldest one,
//not what I want
.limit(20)
.get().addOnCompleteListener { it: Task<QuerySnapshot!>
val messages: List<Message> = it.result.map { it.toObject(Message::class.java) }
println("I fetched ${messages.size} messages : $messages")
if (messages.isNotEmpty()) {
previousOldestMessageTimestamp = *oldestMessage*.timestamp
}
}
}