java - Wait until complete onChildAdded in firebase querys, Android -


i'm new firebase , working on simple project. have method list of teachers firebase , add emails arraylist , 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 working fine. problem returns temp variable before completing adding child.

for example android monitor :

d/return: []

d/teacher: {number=1, name=teacher, email=teacher@new.lk, subject=physics}

d/entry: : data teacher@new.lk

is there way wait until complete thatonchildadded method..?

=========================================================================

i tried in way also. stopped @ inside method.

even didn't go 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; } 

public class mainactivity extends appcompatactivity{ public static bus bus;  protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);              bus = new bus(threadenforcer.main);     bus.register(this);      //call load data firebase     loaddata(); }  private void loaddata(){     databasereference mdatabase = firebasedatabase.getinstance().getreference();     mdatabase.child("my-data").addvalueeventlistener(new valueeventlistener() {         @override         public void ondatachange(datasnapshot datasnapshot) {             //create own object using received data             myobject o = new myobject();             //call method nextmethod()             bus.post(o);         }          @override         public void oncancelled(databaseerror databaseerror) {             log.d("error", databaseerror.tostring());         }     }); }  @subscribe private void nextmethod(myobject o){  } } 

this basic example usage of otto - eventbus. bus.post(o), can call relavant subscribed method.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -