1

In the doc about Cloud Firestore Batched writes I read "You can perform batched writes even when the user's device is offline."

Can this be turned off or is it off by default since the text say "can". The Cloud Firestore Transaction will fail if it´s offline and I would like Batched writes to do the same

I get the feeling I have to check for the result in the OnCompleteListener:

batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // offline/online?? 
    }
});

I think the Doc should contain some explanation on this

Erik
  • 5,039
  • 10
  • 63
  • 119
  • How can batch perform a write operation if it's offline? The docs say it doesn't attempt to retry, so what's it written too? It's just failed silently I assume and the user is ignorant. What's that useful for? – Sam Feb 25 '23 at 11:39

1 Answers1

5

Firestore transactions exist to make it possible to perform atomic read-modify-write operations from the client. In order to make this guarantee the clients need to be online.

Batched writes are a limited kind of transaction that exists specifically to allow users to be able to change multiple documents in an all-or-nothing manner even while offline.

If you don't need or want to be able to write while offline just use a regular transaction: it will handle checking whether or not you're online and will fail if you're offline. You're not obligated to read anything in the transaction.

Checking the result in the OnCompleteListener for a batch write does not work for this purpose. You couldn't prevent anything in that callback because it is only called once the write has been successfully applied to the server.

Gil Gilbert
  • 7,722
  • 3
  • 24
  • 25
  • Thanks I´m migrating from Firebase realtime database and there I used the `updateChildren()` to perform all-or-nothing on 10 nods. As you suggest using Cloud Firestore Transactions, i think that one take only one collection/document so I must run 10 transactions?? – Erik Nov 02 '17 at 19:33
  • 2
    Batched writes are the firestore equivalent of updateChildren. You can put your 10 updates into a single batch and they'll be applied atomically on the server. Your question indicated you didn't want to allow this to proceed unless the user was online. If that's the case then use a transaction instead of a batched write since it forces that requirement. Either method allows you to operate on multiple documents at once. – Gil Gilbert Nov 03 '17 at 16:34
  • ok I see that now the Transaction can take 10 different document. Thanks – Erik Nov 03 '17 at 18:00
  • @GilGilbert "Firestore transactions exist to make it possible to perform atomic read-modify-write operations from the client." -- what about cloud functions? – JavaBeast Sep 23 '18 at 19:46
  • @GilGilbert I have to hide a button after writing batch writes in offline mode, how to do it when OnCompleteListener doesn't work offline? – VINNUSAURUS May 23 '19 at 11:22