How to structure friendship data with firebase?

后端 未结 3 1320
后悔当初
后悔当初 2020-12-16 06:35

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:         


        
相关标签:
3条回答
  • 2020-12-16 07:13

    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
            },
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-16 07:24

    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.

    0 讨论(0)
  • 2020-12-16 07:30

    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

    0 讨论(0)
提交回复
热议问题