Reputation: 53
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.
Upvotes: 5
Views: 4181
Reputation: 9945
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 : “[email protected]”
friends :{
userID_2 : true,
userID_3 : true,
userID_4 : true,
}
},
userID_2 : {
username : “Mikael” ,
useremail : “[email protected]”
friends :{
userID_1 : true,
userID_4 : true,
}
},
userID_3 : {
username : “Lenoard Narish” ,
useremail : “[email protected]”
friends :{
userID_1 : true,
userID_2 : true,
userID_4 : true,
}
},
userID_4 : {
username : “Rob Stark” ,
useremail : “[email protected]”
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
Upvotes: 6
Reputation: 2635
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.
Upvotes: 5
Reputation: 807
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
},
...
}
}
Upvotes: 0