I'm trying to check if a given email exists, with android using firebase I'm doing it 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
You are running first
return retorno[0];
Then it 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 run this function first
query.addListenerForSingleValueEvent()