Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading