Search code examples
swiftgoogle-cloud-firestoreconnection

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?


Solution

  • 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.