0

I'm developing an app and I want every user to have a unique username.

I have associated the unique username with user's UID like this:

mDatabase.child("users").child(user.getUid()).child("name").setValue(userName.getText().toString());
mDatabase.child("users").child(user.getUid()).child("uniqueUserName").setValue(uniqueUserName.getText().toString());

But how would I be able to know that the username which a new user has typed in is available or already taken?

Do I need to run a database search every time or is there some other better techniques?

Please let me know.

AL.
  • 36,815
  • 10
  • 142
  • 281
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • What is the reason you want them to be unique? You already have UIDs... so the username is just a String. Is it because you plan on creating lists of data using those names and duplicates would cause a crash ?? Or is it because you just don't want to confuse users if there are two people with the same name? – Nerdy Bunz Sep 15 '16 at 08:51
  • Also it's confusing that you have THREE things, in fact: 1) UID, 2)"Name", and 3)"UniqueUserName" What are you using these things for? – Nerdy Bunz Sep 15 '16 at 08:52
  • @BooberBunz Twitter also have name and username both... by username I mean something like this '@google'. I just want every user to have their unique username – Hammad Nasir Sep 15 '16 at 08:55
  • Sorry I'm not up on the times and don't use twitter. So... I assume the "Name" is more private (real name), and the "username" is just a normal username that is displayed most of the time in the app? – Nerdy Bunz Sep 15 '16 at 08:57
  • You also didn't say why it needs to be unique. There are different answers for different reasons. – Nerdy Bunz Sep 15 '16 at 09:01
  • It seems like Damirchi's comment holds the best answer, but if the reason is that you just want them to be unique for code reasons, you could concatenate the UID to the username (username = UID + username), but only show a substring (username - UID) when displaying it in the app. – Nerdy Bunz Sep 15 '16 at 09:03
  • Good point on preferring to use `uid` to identify users @BooberBunz. But there are good reasons to want a unique user too. For example: in chatrooms I often want a unique (nick)name for each user, so that you can allow @ mentioning users by that name. Now it would be very cool to only require contextual uniqueness (like Stack Overflow does). But when just starting on the app, global uniqueness is already difficult enough. :-) – Frank van Puffelen Sep 15 '16 at 15:13

0 Answers0