I'm new to Firebase and working on simple project. I have a method to get a list of teachers from Firebase and add their emails to an ArrayList and return.
public ArrayList<String> getTeacherList() {
    temp = new ArrayList();
    Firebase node = ref.child("teachers");
    Query query = node.orderByChild("subject").equalTo(selected_subject);
    query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Map<String,Object> value = (Map<String, Object>) dataSnapshot.getValue();
            Log.d("TEACHER"," "+value.toString());
            Iterator entries = value.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry thisEntry = (Map.Entry) entries.next();
                String key = (String) thisEntry.getKey();
                Object data = thisEntry.getValue();
                if(key.equals("email")) {
                    temp.add(data.toString());
                    Log.d("ENTRY",": DATA "+data.toString());
                }
            }
            
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
        }
    });
    Log.d("RETURN", temp.toString());
    return temp;
}
This method is working fine. But the problem is this returns the temp variable before completing adding child.
For example my Android Monitor is like that :
D/RETURN: []
D/TEACHER: {number=1, name=Teacher, email=teacher@new.lk, subject=Physics}
D/ENTRY: : DATA teacher@new.lk
Is there any way to wait until complete thatonChildAdded method..?
=========================================================================
I tried in this way also. But then it stopped at inside method.
Even it didn't go to Log.d("TEACHER", " " + value.toString()); tag here.
public  ArrayList getTeacherList() throws InterruptedException {
    temp = new ArrayList();
    Firebase node = ref.child("teachers");
    Query query = node.orderByChild("subject").equalTo(selected_subject);
    semaphore = new Semaphore(0);
    query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Map<String, Object> value = (Map<String, Object>) dataSnapshot.getValue();
            Log.d("TEACHER", " " + value.toString());
            Iterator entries = value.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry thisEntry = (Map.Entry) entries.next();
                String key = (String) thisEntry.getKey();
                Object data = thisEntry.getValue();
                if (key.equals("email")) {
                    setArrayList(data.toString());
                    Log.d("ENTRY", ": DATA " + data.toString());
                }
            }
            semaphore.release();
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
        }
    });
    semaphore.acquire();
    return temp;
}
 
    