Firebase: Save UID in the database

0

I would like to know how I can save the uid that it generates for each user in the same database?

I would like this uid to be stored below the sex field, that is to say that it will keep its own uid in every user that believes.

And one more query: I am trying to perform the function of follow and followers, I would like to click on the follow button to be added in both profiles according to the case: profile 1 is following profile 2 and profile 2 is following to profile 1 ...

    
asked by Ibrahim 25.12.2018 в 02:11
source

2 answers

1

Following your answer that is correct, you should check that the userID is not null before sending it to the database, since in any case it can be null, just adding a check before sending it to the database It would be fine.

FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance().getCurrentUser();

if(mAuth != null ){

String user_id = mAuth.getUid();
 DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Usuario");
        DatabaseReference currentUserDB = databaseReference.child(auth.getCurrentUser().getUid());
        currentUserDB.child("uid").setValue(user_id);
}

For the second part of the followers, you can store it under the node of each user with a reference to the user that follows it, in this way you can know the profile of each one and then with a getChildrenCount() you could know the amount of a user's followers

For example, this is a structure I had made for an app similar to what you're looking for

Here you can see that the node followed-by only saves the UIDs of the users that follow me, and follows saves the UIDs of the users that I follow, then, if I want to access the profile of each one, it is enough to go to that user's node with that data and search for its meta data (name, bio, etc.).

In this way, I can also grab and make a getChildrenCount() of each node, as well as followed-by (to know the number of people who follow me) as in the follows (to know how many I follow)

    
answered by 25.12.2018 / 21:47
source
2
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Usuario");
        DatabaseReference currentUserDB = databaseReference.child(auth.getCurrentUser().getUid());
        currentUserDB.child("uid").setValue(user_id);

So I could solve how to get the uid and add it to the database.

    
answered by 25.12.2018 в 02:37