How to structure friendship data with firebase?

I start with firebase and I am trying to structure my data in order to keep friendship between users of my application.

My data is structured as follows:

{ users: { user_1: { name: John }, user_2: { name: Jack }, user_3: { name: Georges }, }, { friendships : { user_1 : { user_2: { name: Jack }, user_3: { name: Georges }, user_3: { user_1: { name: John }, user_2: { name: Jack } } } 

I don’t know if my data structure is good, but I’m wondering what is the best way to update the name in friendship, if the user changes his name, how can I easily update the link in friendship (eyebrows each node?)?

Thanks if someone helps me figure out the right way to do this.

a.

+5
source share
3 answers

Your structure is almost good, but I would change the node friendships to this:

  { users: { user_1: { name: John }, user_2: { name: Jack }, user_3: { name: Georges }, }, { friendships : { user_1 : { user_2: true, user_3: true, user_3: { user_1: true, user_2: true } } 

This is similar to what Dravidian showed, but depending on how many friends you have, it might be better not to keep the friends list of users under the details of the node user account, because if you had 1000+ friends, you had to load 1000 children, even if you just wanted to get one value, like its name.

+4
source

The database structure that you are following is a bit messy and can be a little difficult to navigate. May I suggest: -

  Users{ userID_1 : { username : "Shelly Morgan" , useremail : " sehlly1@yahoo.co.in " friends :{ userID_2 : true, userID_3 : true, userID_4 : true, } }, userID_2 : { username : "Mikael" , useremail : " mike1232@gmail.com " friends :{ userID_1 : true, userID_4 : true, } }, userID_3 : { username : "Lenoard Narish" , useremail : " leo_nar12@gmail.com " friends :{ userID_1 : true, userID_2 : true, userID_4 : true, } }, userID_4 : { username : "Rob Stark" , useremail : " robStar781@gmail.com " friends :{ userID_1 : true } } } 

That way, you only userID friends in this user database, and all you have to do is change the values ​​only in the friends_uid node. To get the friends database: -

1.) just click friends node of that User

2.) listen to all uid friends,

3.) and delete the database with the desired uid to get the database of the corresponding friend

Read the data mining documentation here: https://firebase.google.com/docs/database/android/retrieve-data

+5
source

You cannot store name in the friendships structure only user names. Therefore friendships should look like this:

 { friendships: { user_1: [ user_2, user_3 ], user_2: [ user_1 ] } } 

That way, if you update name , you only update it in one place ( users ). You can cross-reference the username from friendships with the username in users to find the name .

Also, if you use push() for friendship, the structure will change a little, but the same principle applies:

 { friendships: { user_1: { -KPE008BeSzNR5D3W7t3: user_2, -KPE0nF4AW7Lj66xTLUu: user_3 }, ... } } 
0
source

All Articles