I am working on a chat application. I want to get the last 20 messages from the Firebase database and in order, the message was added. For example, if I have sent 3 different message "Hi", "What's up", "Bye". I should get "Hi", "What's up", "Bye" in the same order.
The query I am using is following.
ref.child(Constants.messageListKey).child("-Laray524a9Na-C7zdij").queryLimited(toLast: Constants.messageLimitPerCall).observeSingleEvent(of: .value, with: { (snapshot) in
let currentUserChatList = snapshot.value as? NSDictionary
if let chatList = currentUserChatList?.allKeys {
// parsing the data here
}
}
I even tried using .queryOrderedByKey()
and .queryOrdered(byChild:)
but still the same result.
The result that I am getting is not in order. For example, I am getting "What's up", "Hi", "Bye". The result is not even ascending order nor descending order. It is just a random order.
The scheme which I am using is like this:
-chatList
-autogeneratedKey
-msg = "Some Message"
Is there anything that I am missing? If there is any details that I have missed please let me know to understand my question better.