mergeUnsentWrites property
Not in the official SDKs: a supported opt-in extension that differs from them in what the backend sees (see "What the app gives up" below). Whether a write to a document whose previous write is still pending and was not sent yet replaces that write with one merged write, instead of queuing a second one. Off by default; when off nothing changes.
This keeps the queue of pending writes short for an app that stays offline for a long time and edits the same documents again and again: about one pending write per document instead of one per edit, so a smaller cache, a shorter reconnect and fewer billed writes. The documents on the backend end up the same as when every write is sent, as long as the backend would have accepted each of them.
A write is merged only when all of these hold:
-
It is a single write (
set,updateordelete, or aWriteBatchwith one write), and the newest pending write of its document is also a batch with one write. Batches with several writes are atomic and are never merged into, nor merged. -
That pending write was never handed to the network layer in this session (in particular it is not in flight, and the up to 10 writes queued for the write stream are left alone). Writes queued while the network is enabled but the device is offline are handed out ahead of time, so the first few writes of an offline period stay separate.
-
No other client uses the cache: with a persistent cache, no other client refreshed its row in the cache within the last 30 minutes (unless the database is an
ExclusiveDatabase); clients that share their state are never merged. -
The two writes combine in one of these ways (other combinations stay queued as two writes):
-
The new write is a
setwithout merge (also one withserverTimestamp, but not one withincrement,arrayUnionorarrayRemove, which the local view computes from the value before the write) or adelete: it replaces the pending write, whatever that was. The result of such a write does not depend on the document before it. -
The new write is an
updateor asetwith merge, and the pending write is:- a
set(with no transforms butserverTimestamp): onesetof the pending value with the new fields applied; - an
updateor asetwith merge: one write of the fields of both (the paths of both masks, with the values after both), with the precondition of the pending write; only when the pending write has no precondition (asetwith merge) or both areupdates (if the first one fails on a missing document, so does the second); - a
delete, and the new write is asetwith merge: onesetof the new fields.
Their transforms (
serverTimestamp,increment,arrayUnion,arrayRemove) combine field by field:- a transform of the pending write on a field that the new write writes, deletes or sets to
serverTimestampis dropped (the later write wins); aserverTimestampof the new write stays; - two
arrayUnions of the same field become onearrayUnionof the elements of both, in order and without repeating an element; twoarrayRemoves become onearrayRemoveof the elements of both; - an
incrementof the new write on a field whose value before it is known (one the pending write set, or any field when the pending write is asetor adelete) is written as its result, when both numbers are integers (within +-2^53) or both are doubles; - after an
updateor asetwith merge, a transform on a field that overlaps nothing the other write writes, deletes or transforms (no field equal to it, inside it or containing it) stays as it is, and so does a transform of the new write on a field it writes itself.
Anything else stays queued as two writes, in particular an
incrementafter anincrementof the same field: the backend adds the two numbers one at a time, and a field that holds a double can round differently when the two are added first. Also anarrayUnionafter anarrayRemoveof the same field (or the reverse), anarrayUnionorarrayRemoveof the new write after asetor adelete, a pendingsetwithincrementor array transforms, anupdatethat deletes a nested field at or above aserverTimestampof a pendingset, and any other overlap of a transform with a field of the other write.minimumandmaximumcombine only as the first and last rules above say (overwritten, or on a field the other write leaves alone). - a
The local view of the merged write is the same as with two writes (the documents, their pending-writes flag, query results, and the value under a pending
serverTimestampwithServerTimestampBehavior.previous), except for aserverTimestampof the pending write that stays: until the backend confirms it, its estimate is the time of the newer write. -
The merged write takes the place of the newer write in the queue: writes to other documents queued in between
now reach the backend before the earlier state of this document, which never reaches it at all. The future of
every merged write completes, or fails, with the merged write, and waitForPendingWrites waits for it.
Writes that are already queued when an instance starts with the setting on (queued in an earlier session, or
with the setting off) are merged once, at start, by the same rules and on the same conditions: each write is
merged into the pending write of its document as if it had just been made, in the order of the queue, so the
queue becomes the one the setting would have built from the same writes. This runs in transactions of 500
writes queued as operations of the instance, so reads and writes of the app run between them; no write is sent
to the backend until it is done. A write made meanwhile is merged when the merge of the queue reaches it. The
progress is saved with each transaction: a merge that is stopped (the app is closed, the process is killed)
resumes where it was at the next start, and a queue that was merged is not read again. It does not run while
another client uses the cache, and the queue of a user who signs in later is merged when they sign in. After the
process was killed, the row of the killed client in a SQLite cache counts as another client for up to 30 minutes
(not with an ExclusiveDatabase, such as the web openers): until then neither merge runs, and writes queue as
with the setting off.
What the app gives up:
- Security rules and Cloud Functions triggers see one write instead of several, and never the intermediate
states. Rules that check a transition (
request.resourceagainstresource) see the transition from the first state to the last. - A rejection rejects all the merged writes: the future of each of them fails with the same error.
- Writes to other documents in between no longer see this document in its intermediate state, for example a
rule that reads it with
get(). Do not enable this when rules or functions depend on the order of writes across documents. - The precondition of a write that is replaced by a
setor adeleteis not checked: anupdateof a missing document that is followed by asetsucceeds instead of failing.
Implementation
final bool mergeUnsentWrites;