1

As far as I understand, Firestore, by default, works in such a way that if a write is made, but the client is offline, the write is stored locally, then sent to the server once the connection's restored.

It's an undesired behaviour for me, since:

  • I've got an app that stores some data.
  • Multiple users can update the same data.
  • As far as I understand, if user A stores a local write due to network errors, then user B makes some changes, then user A gets his connection back running, then user's A write will overwrite user's B changes.

Therefore, I'd like to completely disable the offline features. If a write directly to the servers could not be completed for whatever reason, I'd like to have an error thrown, or be signaled in any other way, so I can simply inform the user. How do I do that?

My current code:

    firestore
      .collection("someData")
      .doc(props.id)
      .set({ contentState: content }, { merge: true })
      .catch(x => {
        console.log("Error!");
        setSyncError(true);
      });

The catch func never fires, I assume it's because the buffer-offline-writes capability is enabled by default.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
John Smith
  • 3,863
  • 3
  • 24
  • 42

1 Answers1

1

You mention "Firestore for web" in your question's title.

Note that for the web, offline persistence is disabled by default, as explained in the doc:

When you initialize Cloud Firestore, you can enable or disable offline persistence:

  • For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

  • For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.


For the case when there is no connection between your browser and the Firestore "backend"/service, please see the following Stack Overflow Q/A: How to detect firestore connection failure

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • 1
    From the linked response: "A write that involves a transaction can't be written to local cache - it must be performed while fully online." I've implemented the write as a transaction. Works well. – John Smith Feb 10 '20 at 15:38
  • That's a smart use of a transaction! :-) – Renaud Tarnec Feb 10 '20 at 15:40
  • @RenaudTarnec I don't know If I should post another question - but is this the best answer to this problem? I have the same use case, nothing has changed with this setting. If cache is false, I can still delete/write locally on my app/Flutter. The only two solutions seem to be using a transaction or an onCall to make sure the client is online and guarente the changes are in sync with both parties. – Wesley Barnes Mar 31 '22 at 12:52