5

I'm novice with firebase and I try to structure my data to store friendship between users of my app.

My data are structured like this :

    { 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 OK, but I wonder what is the best way to update a name in the friendships if a user changes his name, how can I easily update the reference in the friendships (brows each node ?) ?

Thank you if someone can help me to understand the right way to do that.

A.

aurevec
  • 53
  • 1
  • 7

3 Answers3

6

The database structure that you are following is a bit messy and might prove a bit hard to navigate through it, Might 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
                 } 
           }

}

In this manner you only store the userID of friends in that users database, and all you have to do is change the values in only friends_uid node.To retrieve the friends database:-

1.) just hit the friends node of that User

2.) listen for every friends uid,

3.) and hit the database with the desired uid to retrieve database of respective friend

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

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I read the firebase documentation and it recommends to avoid nested data : https://firebase.google.com/docs/database/android/structure-data, what do you think about that ? – aurevec Aug 15 '16 at 19:39
  • 1
    Yes if you are going to use it for Real-Time database or so... like for chat , Sending Notification etc. but for storing the static values, its fine.Flatten DataStructures are needed when you need to have a close link to child from parent root like in `Search Engines` OR if your datastructure requires `Weighting`. I think your structure would work fine with this – Dravidian Aug 15 '16 at 20:27
  • OK, I now understand better, thank you for your help. – aurevec Aug 16 '16 at 06:41
5

Your structure is almost good, but I would change the friendships node 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 a better idea to not store the users friendslist under the users account details node because if you had 1000+ friends you would have to download 1000 children even if you just wanted to get a single value like his name.

Linxy
  • 2,525
  • 3
  • 22
  • 37
  • How would you retreive friend data because I am guessing user node will have .read and .write restricted to the actual user. – Newbie Dec 15 '17 at 22:44
0

You shouldn't store name in the friendships structure, only the usernames. So friendships should look more like this:

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

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

Also in case you use push() for friendships, the structure will change a little, but the same principle applies:

{
    friendships: {
        user_1: {
            -KPE008BeSzNR5D3W7t3: user_2,
            -KPE0nF4AW7Lj66xTLUu: user_3
        },
        ...
    }
}
mjr
  • 777
  • 1
  • 10
  • 26