Im checking if an username is repeated or not in my firebase database, when username exists, the value of
usernameExist
inside the method onDataChanged()
would be true, but when I try to access, to the same variable outside the method onDataChanged()
, ever the value is false.
public boolean checkRepeatedUsername(){
refUsernames = FirebaseDatabase.getInstance().getReference();
refUsernames.child(username.getEditText().getText().toString().trim());
refUsernames.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() != null){
usernameExists = true;
System.out.println("Exists username? " + " " + usernameExists); //this print true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
System.out.println("Exists? " + " " + usernameExists); //but this ever print false
if(usernameExists){
return true;
}else{
return false;
}
}