I have an EditText
where I type an username. After I click "REGISTER", a function processes the username to check if it already exists:
public boolean usernameExists (String username) {
database.collection("users").document(username).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete (@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
usernameExists_result = task.getResult().exists();
}
else {
task.getException().printStackTrace();
}
}
});
return usernameExists_result;
}
where database
and usernameExists_result
are both fields. Of course, since the username is the documentID, this function should return true
if the document (and so the username) exists, and false
if it doesn't.
Now, the problem is the function - apart from the first call - always returns the value that it should have returned on the previous call.
For example, if on the first call the function returns true
because the username already exists, and then I type another username that doesn't exist, the function will also return true
. On the next call, it will return false
, because that it what it should have returned on the previous call. And on the next call, it will return whatever it should have returned on the previous, and so on.
Does anyone have an explanation for this behavior? I can provide whatever info is needed.