Can I order firebase addValueEventListeners so that they execute in a specific order? [closed]



Hi I am working on a chat app in Java on Android Studio and trying to read data from firebase Realtime database which is structured like so

{
"Chats":{
    "<chatID>":{
        "<title>",       
        "<lastmessage>"
        "<timestamp>"
        "Users":{                      
            "<user1ID>",
            "<user2ID>"
            }
        }
    },
"Users":{
    "<userID>":{
        "<name>",
        "<email>",
        "Contacts":{
            "<contactID>",
            },  
        "Chats":{                
            "<chatID>"              
            }
        }
    }

}

Currently I have 3 separate event listeners that add read data into a 2D array

ArrayList<ArrayList<String>> chatsDetails; //[("chatID","timestamp",userUID","userName","userEmail"),...]

The code is quite long but the titles of the event listeners (shortest code necessary to reproduce the problem) are like so

/*Listener 1- populate ArrayList with chat ID from Firebase database*/
databaseReference.child("users").child(mAuth.getCurrentUser().getUid()).child("chats").addValueEventListener(new ValueEventListener() {
    
});

/*Listener 2- populate 2d ArrayList with users uid and timestamp from Firebase database*/
databaseReference.child("chats").addValueEventListener(new ValueEventListener() {

    //requires the chat id from listener 1
});

/*Listener 2- whenever values under users changes, update user email and name in 2D array*/
databaseReference.child("users").addValueEventListener(new ValueEventListener() {

    //requires the user uid from listener 2
});

Specific problem: The issue is when I do a Logd to the console, I realise that the listeners do not run in the order I require them to. This results in the name and email of the users not being retrieved and stored into the ArrayList.

Desired Behavior: I want to know if there is any way to make the event listener go one after the other (So the log console should display Listener 1,2,3 executed in that order

I have tried to look for OnCompleteListeners but couldn’t find any for this, and I tried nesting addValueEventListeners which didn’t work. The issue is that when a new is added to the database it triggers all 3 listeners at the same time, so I’m not sure how to make them work in order. Any guidance would be appreciated, thankyou

Answer

 .addValueEventListener()

when the database updates it does something that triggers this function to run .So lets say you have a copy of the Firebase data on the device and everything is sync correctly then NOTHING happens (you dont iven use the internet connection to get data u just ask the db if it did any modifications or no) ,Now all the data you get from you db is from your local db version of your Firebase db. this is just details aboute how firebase works systhematicaly.
If data gets inserted you get the modifications that happened only + the data that didn’t got changed in your “local Firebase db” .
So now you need to store data in a List by clearing and refilling it when a modification happens on “remote Firebase db”.
And then work on the Lists you have on your device .



Source: stackoverflow