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.
How data is structured: it's a JSON tree. All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree. Unlike a SQL database, there are no tables or records.
Firebase security rules, this video does an amazing job explaining them, are a way in which you can sort of create schemas. An important consideration when using these rules is that if you're using the Firestore Admin SDK (used for backend code) then your rules will be bypassed.
The limit you're referring to is the limit for the number of concurrently connected users to Firebase Realtime Database on the free Spark plan. Once you upgrade to a payment plan, your project will allow 200,000 simultaneously connected users.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With