I am making an Android application using Firebase
realtime database. When a new user registers on my app, that user's data is saved in the Firebase database.
A user has to provide the following details to register:
- Full Name
- Username
- Password
Database Structure
Whenever a new user tries to register, I have to make sure that each user's username is unique so I check the database if the username
entered by the user already exists in the database or not.
To do this, I wrote the following method:
private boolean usernameExists(String username) {
DatabaseReference fdbRefer = FirebaseDatabase.getInstance().getReference("Users/"+username);
return (fdbRefer != null);
}
My logic behind this method is that if getReference
method cannot find reference to the specified path, it will return null so that I can return whether fdbRefer
is null
or not. If fdbRefer
is null
, then it means that username
doesn't exist in the database.
Problem
Problem with this method is that it always returns true
whether the entered username
exists in the database or not. Which led me to believe that fdbRefer
is never null
.
This brings me to my question...
Question
What does getReference
method return when it can't find the specified path in the firebase
database and what's the correct way to check if the username
already exists in the database or not?