How to await the return of information using Firebase Realtime Database

Asked

Viewed 66 times

0

I’m having trouble working with asynchronous Firebase Database data

I need to identify the type of logged-in user:

public Static String getTipoUsuario(){ User = query User(); Return usuario.getType(); }

public static Usuario consultarUsuario(){

    DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
    DatabaseReference usuario = firebaseRef.child("usuarios").child(getIdUsuario());

    usuario.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                usuarioLogado = dataSnapshot.getValue(Usuario.class);
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

    return usuarioLogado;
}

What happens is that the method always returns NULL, because the application executes the code before the return of the database information.

How can I make it return only with the certainty that the query has been made ?

  • Where you are creating this user variableLog?

  • Global variable of a user class

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.