2

I have a use case where I log each activity open count in a firestore document (activity_name->field, and count->value)... So I wanted to know, when the user is offline and each of his activity navigation is stored in firestore cache, as soon as the user gets online and firebase sdk syncs the changes to the main database, does firestore record the synced changes as a single write or it sees the various individual field changes since the last change and record as multiple writes?

1 Answer 1

3

The writes are queued up in the client and delivered individually, so there will be a cost of one write for each document that was written offline.

The important issue here is not so much the billing as it is the evaluation of security rules. It's entirely possible a series of 5 writes might actually only result in 4 successful writes and 1 failure due to the violation of a security rule. If those writes were actually compressed into a single write, that would potentially cause everything to fail, which would be undesirable (you'd likely want as many writes to succeed as possible).

Sign up to request clarification or add additional context in comments.

5 Comments

Just to be clear, in this case it seems that there is only one document, correct me if I'm wrong. Issuing multiple field changes for a single document is considered as a single write operation, but here those changes are queued up. This means that the same database fields will be updated multiple times, resulting in billing for multiple writes. It could be possible to perform an operation which sums up the changes, which would result in a single write operations to the document's fields. Cloud Functions could be used for this, as an example.
@PYB The issue here is the queuing up of offline writes. Cloud Functions can't be invoked while offline, though, so it's not really an option. If the OP wants to coalesce the writes, they'll have to manage their own offline state and commit the final write later.
I meant that the Cloud Function could be triggered when the user logs back in to aggregate the changes and perform the write, but it might indeed be simpler to just have the the app coalesce the writes and execute the operation itself.
But then this is not the case for document reads in firestore. Changes to an online document are seen as single read in client after the listener gets attached again (If originally removed) but this is not the case for writes @Doug
The Firestore client SDKs don't give a hard guarantee that a listener will see each and every change as a callback to the client. The synchronization is based on final state, not a series of deltas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.