3
I’m trying to check if a given email exists, with android using firebase I’m doing this way :
public boolean existeEmail(String email) {
final boolean[] retorno = new boolean[1];
final DatabaseReference databaseArtists = FirebaseDatabase.getInstance().getReference();
Query query = databaseArtists.child(colecao).orderByChild("email").equalTo(email);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
retorno[0] = true;
else
retorno[0] = false;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return retorno[0];
}
Problem
Is running first
return retorno[0];
And then executes:
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
retorno[0] = true;
else
retorno[0] = false;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
and is always returning false. How do I first execute this function
query.addListenerForSingleValueEvent()
Classic problem of asynchrony
– Rosário Pereira Fernandes
I need to put her in a class.
– Joy Peter
When working with asynchronous Apis, such as Firebase, the developer is forced to think differently than the traditional sequential way. If a little more context were added to the question (where the method is used, for example) it would be easier to indicate alternatives to get the desired result.
– Leonardo Lima
Putting codes into Activity creates a lot of code repetition.
– Joy Peter
Have you seen my answer? Need some more information?
– ramaral
I did, sorry for the delay, I was away.
– Joy Peter