I am creating a chat application, and the last chat message is called up in the chat / chat list to show it just like in any other messaging application.
However, when the chat is open, all chat messages are retrieved using messagesDbRef.orderByChild("time").addChildEventListener(...)
, and the callback is onChildAdded
instantly called for the last chat message (the one that was received in the chat list) that has the maximum value for the field time
, while all other messages are then retrieved from the database in increasing order of field values time
.
In the example of messages marked from 1 to 5, this leads to their addition to the RecyclerView in the order of [5, 1, 2, 3, 4], the last being 5.
However, when the chat is closed and opens again, the order is correct [1, 2, 3, 4, 5].
How can i fix this? Is there a way to force all messages to reload when opening a chat?
EDIT:
Here is a minimal working example that reproduces the problem:
Data in the real-time database:
testing: {
abc123: {
name: "first",
time: 100
},
abc456: {
name: "second",
time: 200
},
abc789: {
name: "third",
time: 300
}
}
the code:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("testing");
ref.child("abc789").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Stuff lastStuff = dataSnapshot.getValue(Stuff.class);
Log.i("Testing", "retrived child " + lastStuff.name + " with time " + lastStuff.time);
ref.orderByChild("time").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Stuff stuff = dataSnapshot.getValue(Stuff.class);
Log.i("Testing", "name: " + stuff.name + ", time: " + stuff.time);
}
...
});
This leads to the output:
I / Testing: returned child third with a age of 300
I / Testing: name: third, time: 300
I / Testing: name: first, time: 100
I / Testing: name: second, time: 200
However , I noticed that if I use addListenerForSingleValueEvent
instead addValueEventListener
, the problem will disappear and the order will be correct. I probably just have a listener open somewhere in my application.
In any case, I do not think that the cached value should subsequently affect the search order.