I have this type of structure in my firebase real time db. I want to fetch multiple child's last item's data for only once.
I am following below approach :
for (roomId in roomInformation.keys) {
// roomInformation will contain some of room ids
// to fetch from firebase db(not all roomIds from Messages)
val messageObserver = object : ValueEventListener {
...
}
FirebaseDatabase.getInstance().reference
.child("Messages")
.child(roomId)
.limitToLast(1)
.addListenerForSingleValueEvent(messageObserver)
}
I don't want to fetch all childs (roomIds)
from Messages
Collection. This looping approach to get data from firebase is working but I want to fetch required records in a single callback (without for loop). Is there any possibility to do this or Is there any other better approach for same?
UPDATE: More clarification:
As per image: there is one Messages collection which include id's of Rooms and inside that rooms there are thousands of messages. I want to fetch all room's last message. So as per query I applied: Messages -> RoomId -> limitToLast(1) so It will give me last message info of particular room. User might join with hundreds of rooms and initially when user do login I want to display all rooms last message. so I am fetching messages of rooms by using for loop so Firebase will give me a single room last message at a time but I want all rooms last messages at a time. Here all room in the sense, Messages Collection is having 300 Rooms and If UserA is joined with 50 rooms then I only wants last messages of 50 rooms