1

I came across unique use case where following feature will come in very useful.

In essence I have components that are listening for specific changes in a document, my security rules are set up in a way where reads are allowed, but all writes are disabled as database updates are only possible through cloud function, hence I was researching the docs to find if it is possible to do something like this.

/**
 * This update should only happen locally / offline in order to update state of components
 * that are listening to changes in this document
*/
myDocumentRef.update({ name: 'newname' });

/**
 * Then cloud function is called that performs validation and if correct
 * updates document with new data (same as one we set offline above).
 * Once that is done listening components will receive new data. If unsuccessful
 * we would set document to old data (again offline) and show error.
*/
await myCloudFunction();

Hence my question: is it possible to perform that update (and only update) locally / offline? This is basically "optimistic update" flow utalising firebase as global store of sorts.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • You can create your own strategy. This this answer https://stackoverflow.com/questions/47843218/offline-issue-with-firestore-vs-firebase/65667256#65667256. – mahdi shahbazi Jan 11 '21 at 12:58

1 Answers1

1

Firestore does not provide a concept of "local only" updates. All writes will be synchronized with the server at the earliest convenience. If a write eventually fails, the promise returned by the API call (if still in memory) will be rejected. If the sync happens after the app restarts, the promise is lost, and you have no way of knowing if the write succeeded. Local writes that eventually fail will be reverted in the local cache.

What you will have to do instead is write your data in such a way that the local listener can tell the stage of the write, possibly by writing metadata into the document to indicate that stage, cooperating with the Cloud Function on keeping that up to date.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441