user6564874
user6564874

Reputation:

Firebase Database: How to save a list of friends per user?

Every User has a list of items with the same key like for example friends. I know this example is wrong because every key should be unique. How can I do that in the JSON Format?

 {
  "users" : {
    "jmEcUjPaXVXHdsaddasdFrh1" : {
      "Name" : "John",
      "friends" : {
        "Name" : "Peter",
        "Name" : "Bob",
        "Name" : "Lisa"
      }
    }
  }
}

I tried this but I am not sure if this is a good solution

{
  "users" : {
    "jmEcUjPaXVXHdsaddasdFrh1" : {
      "Name" : "John",
      "friends" : {
        "Peter" : "true",
        "Bob" : "true",
        "Lisa" : "true"
      }
    }
  }
}


#askFirebase

Upvotes: 2

Views: 2954

Answers (2)

iamtheib
iamtheib

Reputation: 204

What you're looking for is to store array of friends.

{
  "users": {
    "jmEcU..." : {
      "Name" : "John",
      "friends" : [
        "Peter",
        "Bob",
        "Lisa"
      ]
    }
  }
}

However, there are some concurrency issues with using an array with distributed data. I'll suggest you really read this post to get an understanding of the implications of using an array with Firebase Database.

If after going through this section of the post, and you determine you cannot use an array, here are a couple of suggestions for a new structure

Structure 1: Use the friend's name as the key. You can use anything as the value, as long as it is not null. You can loop through all the children of friends and get the keys.

Pro: Very quick to find out if a user is friends with another user - /users/{id}/friends/{name} exists

Con: Each friend's name has to be unique

{
  "users": {
    "jmEcU..." : {
      "Name" : "John",
      "friends" : {
        "Peter" : true,
        "Bob" : true,
        "Lisa" : true
      }
    }
  }
}

Structure 2: Using Firebase's Push to create unique ids and use as keys, with the friends' names as values. You can loop through all the children of friends and get the values.

Pro: Friends names don't have to be unique

{
  "users": {
    "jmEcU..." : {
      "Name" : "John",
      "friends" : {
        "-JGnfnf..." : "Peter",
        "-JGnfoi.." : "Bob",
        "-JGnfuy.." : "Lisa"
      }
    }
  }
}

If what you need from the friend is more than just the name, I would advice to store the friend's user-id, instead of an object representing the friend's details. If your user-ids are unique, this should solve the con of Structure 1, and make it the better of the two options.

Upvotes: 4

iOSGeek
iOSGeek

Reputation: 5587

Try this structure:

  "users" : {
    "{user_1}" : {
      "email" : "[email protected]",
      "firstName" : "Geek",
      "friends" : {
        "{user_2}" : {
          "status" : "pending",
          "with" : {
             "firstName" : "Anna", 
            "profileImage" : "http://..."
          }
        }
      }
    } ,
    "{user_2}" : {
      "email" : "[email protected]",
      "firstName" : "Anna",
      "friends" : {
        "{user_1}" : {
          "status" : "pending",
          "with" : {
             "firstName" : "Geek",
             "profileImage" : "https:..."
          }
        }
      }
    }
  }

Upvotes: 1

Related Questions