7

Can any one help me to detect internet connection with Firebase Database using Swift 3? I am using this function to download data from database.

  func loadData(){


    Ref=FIRDatabase.database().reference()

    Handle = Ref?.child("Posts").queryOrdered(byChild: "Des").queryEqual(toValue: "11").observe(.childAdded ,with: { (snapshot) in

        if  let post = snapshot.value as? [String : AnyObject] {

            let img = Posts()

            img.setValuesForKeys(post)

            self.myarray.append(img)

                self.tableView.reloadData()

        }else {


        }

    })

}
AL.
  • 36,815
  • 10
  • 142
  • 281
Zaid Mustafa
  • 211
  • 1
  • 4
  • 12

1 Answers1

21

If you want to detect whether your app has a connection to the Firebase Database backend, you can listen for /.info/connected. This example from the Firebase documentation on detecting connection state should do the trick:

let connectedRef = FIRDatabase.database().referenceWithPath(".info/connected")
connectedRef.observeEventType(.Value, withBlock: { snapshot in
    if let connected = snapshot.value as? Bool where connected {
        print("Connected")
    } else {
        print("Not connected")
    }
})

Swift 3.1

let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
    connectedRef.observe(.value, with: { snapshot in
    if let connected = snapshot.value as? Bool, connected {
        print("Connected")
    } else {
        print("Not connected")
    }
})

Objective C

FIRDatabaseReference *connectedRef = [[FIRDatabase database] referenceWithPath:@".info/connected"];
    [connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
   if([snapshot.value boolValue]) {
    NSLog(@"connected");
  } else {
    NSLog(@"not connected");
  }
}];
Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This doesn't seem to work if the app is launched with no internet connection. – Raphael Oct 04 '17 at 22:11
  • 2
    It seems that the response block doesn’t execute if the app is launched with no internet connection. Might be an issue with the version of firebase I’m using. – Raphael Oct 05 '17 at 00:14
  • 4
    This does not work if I enable network link conditioner on a profile with 100% packet loss, it always shows as connected when in fact it has no connection to firebase. Any ideas how to get around that? – gomezluisj Oct 11 '17 at 18:29
  • Frank, Is there something similar for testing FireStore connection? – BobCowe Nov 18 '18 at 21:28
  • Firestore uses a different protocol, so there's no similar approach. The docs describe a presence solution for Firestore that uses the Realtime Database: https://firebase.google.com/docs/firestore/solutions/presence – Frank van Puffelen Nov 18 '18 at 21:35
  • Swift 5 and newer Firebase: let connectedRef = Database.database().reference(withPath: ".info/connected") connectedRef.observe(.value, with: { snapshot in if let connected = snapshot.value as? Bool, connected { print("Connected") } else { print("Not connected") } }) – Jkrist Dec 15 '22 at 16:16