1

I want to check if an device (in this case an IOS device) has access to the Firebase Realtime Database.

So I have created this method:

private func isConnected(completionHandler : @escaping (Bool) -> ()) {
   let connectedRef = Database.database().reference(withPath: ".info/connected")
       connectedRef.observe(.value, with: { snapshot in
            completionHandler((snapshot.value as? Bool)!)
       })
}

This method is working perfect. And I'm using it like this in every method where I'm doing something with the realtime database:

isConnected { (connected) in
    if(connected){
         //do your things
    } else {
       print("network error")
    }                       
}

But now my question, what if you get the network error. How do you know if your device has no internet connection or the Firebase service is down? What is a good approach to do this? Because the only thing I know right now is that I can't reach the realtime database.

So my question is, how can I get more detailed information? Isthe device offline? Is firebase down? etc etc.

da1lbi3
  • 3,745
  • 5
  • 23
  • 53
  • 1
    Use reachability. – Tarvo Mäesepp Jul 24 '17 at 22:50
  • 1
    For an Android client, a connection exists only when needed. The [documentation](https://firebase.google.com/docs/database/android/offline-capabilities#section-connection-state) explains: _On Android, Firebase automatically manages connection state to reduce bandwidth and battery usage. When a client has no active listeners, no pending write or onDisconnect operations, and is not explicitly disconnected by the goOffline method, Firebase closes the connection after 60 seconds of inactivity._ I don't have experience with iOS, but would expect it to be the same... – Bob Snyder Jul 25 '17 at 00:57
  • 2
    So when `.info/connected` returns false, is may mean a connection is not possible, or a may mean a connection is not needed. – Bob Snyder Jul 25 '17 at 00:58

1 Answers1

1

Firebase's .info/connected only signals whether your app is connected to its Firebase Database backend. It does not detect general network connectivity.

To determine the latter, try one of the many answers from [this list]. Including How to check for an active Internet connection on iOS or OSX?, Easiest way to detect Internet connection on iOS?, and Check for internet connection with Swift.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645