Android & Firebase Async tasks

0

I have a class Java that I overwritten the toString() method inside this method before returning I consult the fireBase and fill in an object. My problem is the return of toString() ends up running before the firebase has filled the object. How can I tell the class to wait for the object to be filled before it returns

public class exom(){
 private String ID;
 private String horaInicio;
 private String horaFim;
 private String IDEvento;
 // Objeto que deve vir do firebase
 Objeto obj;
  //Getters and setters....
   @Override
   public String toString() {
     FireDB fireDB = new FireDB(null);
     fireDB.getMdatabase().addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           for (DataSnapshot child : dataSnapshot.getChildren()) {
            obj = child.getValue(Objeto.class);
           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

     return horaInicio + " - " + horaFim + " - "+ obj.getNome();
   }
}
    
asked by anonymous 13.06.2017 / 16:29

1 answer

0

In reality it is not a good practice for you to perform this type of query that needs to "wait" on the main thread. As for the firebase, unfortunately (or fortunately) it does not have a synchronous form to get data.

You should fill in the object before doing any comparison with toString ();

    
14.06.2017 / 15:46