Instead of using push()
to generate unique keys for your users, use the user
object's uid
to have user nodes in your database. You can get the uid
of the current logged in user by calling the following line :-
uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
And then calling the next line to use it as a node in your database :-
mDatabase.child("users").child(uid).child("username").setValue("ABC");
This works great because :-
uid
is unique for each user and is the easiest and most robust way to have and identify user nodes in the database - specially at multiple locations.
uid
of logged in user can be easily obtained from the client Android app using the above line. Therefore, you won't need to specifically save and tally the unique ids on the client. This is recommended not only for simplicity, but also for security.
So, uid
nodes are ideal for user nodes in your database. But, for all other purposes, push()
is still the recommended approach.