Google Firestore .set() doesn't calling failure listener when connection lost

I’m trying to catch a case when the app creating a document, but at that moment the connection was lost. So I expect to OnFailureListener or OnCanceledListener get called, but no one callback called at all in this case. How can I catch errors while creating a document while .add() or .set()? Thanks.

>Solution :

As already @ArpanSarkar mentioned in his comment, neither the OnFailureListener nor the OnCanceledListener gets triggered when there is a loss of network connectivity (there is no network connection on the user device). So the Firestore SDK doesn’t throw an Exception when there is no internet connection. And it makes sense since Firestore is designed to work offline. Behind the scenes, Firestore SDK tries to reconnect until the devices regain connectivity. That being said, please note that all the writes that are made while offline, are added to a queue, and once the device regains internet connectivity everything will be synced with the Firebase servers.

On the other hand, an Exception will be thrown, only when the data has been committed (or rejected) on the Firebase server, for example, due to improper security rules.

Leave a Reply