John
John

Reputation: 437

Duplicating nodes

In my chat application I have users and message.

Database structure:

enter image description here

Each message consits of a messageand the userid of the sender called senderUid. If I wanted to display a message and along with the image of the sender (imageUri), would it then be most efficient to query the users node using the senderUid? Or could I add the imageUri under each message, so that I only need to query message node?

Upvotes: 0

Views: 31

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138999

If I wanted to display a message and along with the image of the sender (imageUri), would it then be most efficient to query the users node using the senderUid?

If you want to display the message of the sender along with his image, using your actual database structure, you need to query it twice. First time you need to query to get the uid from the message object and second, you should query the users node based in the uid, to get the image url. This implies a nested query but won't be a problem in case of Firebase.

Or could I add the imageUri under each message, so that I only need to query message node?

In the same way you have added the uid of the sender, you can also add the image url. In this case, you'll reduce the number of queries that you perform but you'll increase the size of the message object. Again, this won't be a problem in case of Firebase. So I recommend you go ahead with this solution.

However, if you consider at some point in time to try using Cloud Firestore, here you can find a tutorial on how to create a complete and functional Firestore Chat App.

Upvotes: 1

Anupam
Anupam

Reputation: 2853

You can save the image uri in the message node instead of making another query. This will reduce the time complexity as well as another loop for each and every message in the conversation page.

Upvotes: 1

Related Questions