Ing. Ron
Ing. Ron

Reputation: 2095

SWIFT - How to detect Firestore connection errors / offline connection

When the users sets the internet connection to offline I'm getting the following error message to the Xcode console:

7.8.0 - [Firebase/Firestore][I-FST000001] WriteStream (11ed16098) Stream error: 'Unavailable: DNS resolution failed'

But how can I detect the status of the connection programmatically? I don't find any document at the Firestore documentation regarding that topic?

Upvotes: 1

Views: 1303

Answers (2)

timbre timbre
timbre timbre

Reputation: 13960

Normally, in this state it's best to monitor network connection state of the device, instead of looking for a particular error. It won't tell you when Firebase is failing, but it will tell you when device is offline, so you can expect the failure. You can use NWPathMonitor to do that. Here is the implementation example.

And even nicer option is to enable offline capabilities of Firebase, which allows you to not worry if user is offline, and let Firebase to handle synchronization.

Upvotes: 3

slushy
slushy

Reputation: 12385

Firestore doesn't generate network-connection errors because Firestore is offline capable so it will retry the write (some writes, not all) when connection returns. Therefore, you'd have to determine network availability independently from Firestore.

However, if you want a more traditional approach to this task, one that fails when there is no connection and does not automatically retry when connection returns, you can perform it within a Firestore transaction since those are not cached locally. Transactions do come with automatic retry, however, but not for connectivity reasons. Transactions can retry automatically when the documents involved in the transaction are modified (by other clients) before the transaction finishes to guarantee they are atomic.

Upvotes: 1

Related Questions