I have a Flutter app that lets users rent items from eachother with Firestore RTDB. In my rental document, I have a field status
that determines the status of the rental (think of it like shipping an item, where items can have status of 'ordered', 'shipped', 'delivered' etc). My status
variable is a number between 0 and 5, and each number represents a different phase. When the status
variable changes, I want to notify the other user in the rental with a push notification. But I don't know which of the following methods is best.
The first way is to use a cloud function that triggers every time the rental document is updated. But I only check for the
status
field. It would look something like this:exports.notify = functions.firestore.document('rentals/{rentalId}') .onUpdate(async (snapshot, context) => { const oldSnap = snapshot.before.data(); // previous document const newSnap = snapshot.after.data(); // current document // status changes from 0 to 1 if (oldSnap.status === 0 && newSnap.status === 1) { // do something } })
The one downside I can think of is I would have to do another read to get the device push token of the other user. Also, for every rental document update this cloud function will trigger, and ultimately may not even need to execute in the first place
The other way would be to have a
notifications
collection that stores notifications, and have a cloud function that triggers when a new notification document is added. Then, on the client side, when the user taps a button, update thestatus
in the rental as well as create a new notification document.Firestore.instance .collection('rentals') .document(rentalId) .updateData({'status': newStatus}); Firestore.instance.collection('notifications').add({ 'title': title, 'body': body, 'pushToken': <TOKEN HERE>, });
In comparison to method 1, this does an extra write instead of a read.
Which method is better?