How to structure friendship data with firebase?

后端 未结 3 1324
后悔当初
后悔当初 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: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

提交回复
热议问题