So I have a root in the database like this:
users {
"user1" {
username: "username1",
email: "email@email.com"
}
"user2" {
...
}
...
}
Now, I want to register a new user, so for that I need to check if the username that the new user choose is already chosen. For that I created a single time event and gather all usernames in a string list:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> res = new ArrayList<String>();
Iterable<DataSnapshot> users = dataSnapshot.getChildren();
for (DataSnapshot user : users) {
res.add(user.child("username").getValue(String.class));
}
// No way to return anything
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
But I have a problem, that method is asynchronous. I need to gather all usernames and know if any exists in that list synchronously.
Is there any way to achieve this?