Home > Blockchain >  How to cancel the write operation if there is no internet? Firestore
How to cancel the write operation if there is no internet? Firestore

Time:12-06

firebaseFirestore.collection("Test").document("test123").set(hashMap);

I want to write this document inside the collection, But when the user was offline it is scheduled the operation until the Internet returns. But I want if there is no internet then it must cancel the write operation even if the internet is back.

If there is internet then will do the natural job and add the document to the server but if there is no internet I want to cancel the write operation even if the internet is back.

CodePudding user response:

Not having an internet connection is not considered an error condition by the Firestore SDK. Instead if will keep the pending write in its local memory cache (and on disk, unless you disabled that) and send it to the server once the connection is established.

There is no configuration option or API call to prevent the Firestore SDK from caching pending writes. So you'll have to do something in your application code to accomplish the behavior you want.

The most common options are:

  • Detect whether there's an internet connection before writing to the database, and only performing the set call when there is a conneciton.
  • Add a completion listener to the set call, and set up a timer for how long you think a write should reasonably take. If the timer goes off before the write was completed, clear the local cache to cancel all pending writes.
  • Related