I get the Alex Mamo's Solution in order to check whether a unique value exists in my database but the snapshot gets always null. The solution is here:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = rootRef.child("Users").child("Nick123");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
//create new user
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userNameRef.addListenerForSingleValueEvent(eventListener);
And my database looks like this:
users
(auto-generated push token ID)
fullname
username
gender
...
I don't know whether it's the right method but I used push() method for adding the user object to the database and this method generates push token ID like above.
So, in order not to create duplicate users, what should I change with the above solution? Is there a better method to do this? Maybe like checking the firebase uid with some firebase auth methods?
UPDATE
My database screenshot:
As you see the string just below 'users' is not the same as user's uid.