问题
Cannot get Firebase to identify a username that has already been entered. The code below shows that I am trying to compare the username entered with a DataSnapshot of my database. The code doesn't seem to pick up usernames that are entered. Is the code wrong?
private boolean mSwitch = false;
public void checkInfo(View view){
checkUsernameIsUnique();
checkInformation();
if(mSwitch) {
Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
saveUserInformation();
startActivity(intent);
}
}
public void checkUsernameIsUnique(){
String username = Username.getText().toString().trim();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usernameRef = rootRef.child("user_profile_info").child(userID);
Query checkName = usernameRef.equalTo(username);
checkName.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = Username.getText().toString().trim();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if(name.equals(ds.child("username").getValue(String.class))){
Username.setError("Username Taken");
Username.requestFocus();
} else {
Toast.makeText(ProfileActivity.this, "Registered User", Toast.LENGTH_SHORT).show();
mSwitch = true;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
if(Username.getText().toString().equals("")){
Username.setError("A Username is Required");
Username.requestFocus();
}
if(Username.length() < 5){
Username.setError("Username must be 5 characters minimum");
Username.requestFocus();
}
}
On button click I wish to be able to direct the user to the error, and until it is unique the page should not be pushed to the next activity.
The JSON tree if it helps
"user_profile_info" : {
"8FUV95JorVT26Gd1QFNUbxEf8O93" : {
"date_of_birth" : "2/5/1925",
"diet" : "Moderate",
"drinker" : "No",
"education" : "Masters Degree",
"exercise" : "3 - 5 Hours a Week",
"forename" : "Ben",
"gender" : "Male",
"have_alzheimers" : "No",
"have_diabetes" : "No",
"have_parkinsons" : "No",
"history_alzheimers" : "No",
"history_diabetes" : "No",
"history_parkinsons" : "No",
"marital_status" : "Single",
"smoker" : "No",
"surname" : "Dover",
"username" : "Maturity101"
}
回答1:
addListenerForSingleValueEvent is an asynchronous operation. After the callback onDataChange() is set the code continues running while you have not fetched any usernames yet. By the time you check for mSwitch value its always false, even if the username is OK.
To make it working you should move your
checkInformation();
if(mSwitch) {
Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
saveUserInformation();
startActivity(intent);
}
to the end of the onDataChange() callback.
Also it would be better to first for check empty string and length before you query for documents. Just to cut useless data downloaded from the database.
But it seems that it still won't be working because of the for loop. Try smth like that:
mSwitch = true; // as its OK
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if(name.equals(ds.child("username").getValue(String.class))){
mSwitch = false;
Username.setError("Username Taken");
Username.requestFocus();
break;
}
}
...
if (mSwitch) startActivity();
else showErrorToast();
来源:https://stackoverflow.com/questions/57837902/how-to-check-if-username-exists-in-my-firebase-database-already