title: Wait until complete onChildAdded in firebase querys, Android
body: <p>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.</p>
  <pre><code>public ArrayList&lt;String&gt; getTeacherList() {
      temp = new ArrayList();
  
      Firebase node = ref.child(&quot;teachers&quot;);
      Query query = node.orderByChild(&quot;subject&quot;).equalTo(selected_subject);
  
      query.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
              Map&lt;String,Object&gt; value = (Map&lt;String, Object&gt;) dataSnapshot.getValue();
              Log.d(&quot;TEACHER&quot;,&quot; &quot;+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(&quot;email&quot;)) {
                      temp.add(data.toString());
                      Log.d(&quot;ENTRY&quot;,&quot;: DATA &quot;+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(&quot;RETURN&quot;, temp.toString());
      return temp;
  }
  </code></pre>
  <p>This method is working fine. But the problem is this returns the <code>temp</code> variable before completing adding child.</p>
  <p>For example my Android Monitor is like that :</p>
  <blockquote>
  <p>D/RETURN: []</p>
  <p>D/TEACHER:  {number=1, name=Teacher, email=teacher@new.lk, subject=Physics}</p>
  <p>D/ENTRY: : DATA teacher@new.lk</p>
  </blockquote>
  <p>Is there any way to wait until complete that<code>onChildAdded</code> method..?</p>
  <p>=========================================================================</p>
  <p>I tried in this way also. But then it stopped at inside method.</p>
  <p>Even it didn't go to <code>Log.d(&quot;TEACHER&quot;, &quot; &quot; + value.toString());</code> tag here.</p>
  <pre><code>public  ArrayList getTeacherList() throws InterruptedException {
      temp = new ArrayList();
  
      Firebase node = ref.child(&quot;teachers&quot;);
      Query query = node.orderByChild(&quot;subject&quot;).equalTo(selected_subject);
  
      semaphore = new Semaphore(0);
  
      query.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
              Map&lt;String, Object&gt; value = (Map&lt;String, Object&gt;) dataSnapshot.getValue();
              Log.d(&quot;TEACHER&quot;, &quot; &quot; + 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(&quot;email&quot;)) {
                      setArrayList(data.toString());
                      Log.d(&quot;ENTRY&quot;, &quot;: DATA &quot; + 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;
  }
  </code></pre>