I am creating a sample Twitter App in iOS using Firestore. Currently, I have the following root level collections:
- users
- timelines
In the timelines collection, I have tweets based on the userId and each Tweet also holds a userInfo map, which contains the information about the user who tweeted.
The problem is that if the user updates their name from Steven to Stephan then I can easily update the users collection but I need to go through all the tweets of the user and then update the name. I also need to go through all the replies from that user in other tweets and update the name.
This seems like too much work!
Any ideas how to handle this scenario in Firebase Firestore.
CodePudding user response:
Any ideas how to handle this scenario in Firebase Firestore.
A classical approach to synchronize your different sets of data (i.e. Documents in different Collections) in a denormalized NoSQL Firestore database is to use Cloud Functions.
If the user updates their name from Steven to Stephan
You can trigger a Cloud Function each time a Firestore document is updated or deleted. In this Cloud Function you'll query for the documents to update (i.e. query all the docs in the timelines
collection where name === "Steven"
) and update them (name = "Stephan"
) with e.g. a batched write.