How to structure friendship data with firebase?

后端 未结 3 1323
后悔当初
后悔当初 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
            },
            ...
        }
    }
    

提交回复
热议问题