0
userRef.child(userId).observeSingleEvent(of: .value, with: { snapshot in
        //some codes
})

This is my code for getting some user data from the firebase database. My question is how to get the error if there is an error (like network error, request timeout, unknown error)? The observeSingleEvent has no completionBlock compared to others (setValue, updateValue etc.)

I also tried:

userRef.child(userId).observeSingleEvent(of: .value, with: {(snapshot) in
    // print something
} , withCancel: {(error) in
    // print something
})

still wont go inside withCancel.

jay123456
  • 131
  • 1
  • 1
  • 10

3 Answers3

1

Is this what you are looking for?

userRef.child(userId).observeSingleEvent(of: .value, with: { (snapshot) in
        print("Worked")           
    }) { (error) in
        print("Didn't")        
    }

You are also able to to make another check of incase your made a mistake in your observation.

if snapshot.value is NSNull{
     //snapshot is null
} else{
     //Not null
}
Torewin
  • 887
  • 8
  • 22
  • I already tried that first one and I print the error but nothing happens. I also debug it and it didn't went into the part with print. – jay123456 Jul 05 '18 at 03:17
  • Oh okay, so if you have something like me edited answer - do you get either. – Torewin Jul 05 '18 at 03:25
  • If you are sure that your observer is being called, and I am assuming you are starting the app without the connection to the internet then you will have to do one of two things. Check your connection: https://firebase.google.com/docs/database/ios/offline-capabilities or do it manually and check if the phone is connected to the internet. – Torewin Jul 05 '18 at 03:30
  • 1
    The first answer works for me if the error is permission_denied from security rules. Thanks! – Stotch Nov 15 '22 at 03:08
0

Just like there is an observe(of: with: _ withCancel:_), there is also a observeSingleEvent(of: _ with: _ withCancel: _).

If the code with a cancel block doesn't work for you, update your question to show what you tried and describe clearly what behavior you expected, and what behavior you got instead.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi, I saw your answer here (https://stackoverflow.com/questions/41661137/how-to-detect-internet-connection-with-firebase-database-using-swift3/41663192#41663192) and I just used your logic for this problem of mine. But I have a question for your answer, how come when I run that code it went from not connected and then connected afterwards, even if their is already an internet connection when the app launches? – jay123456 Jul 05 '18 at 08:42
  • The `.info/connected` flag doesn't determine whether there is an internet connection. It determines whether the app/client is connected to the Firebase Database servers. That takes a moment after the app starts, so usually the value starts as `false` and then becomes `true`. Sometimes it even toggles a few times before settling. – Frank van Puffelen Jul 05 '18 at 13:07
0

This is what I did.

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")
        }
    })

Everytime I request something to/from the database, I do this to check if I'm connected to the database. If connected, make the request. If not, prompt an alert or something. But for the sudden internet disconnection while their is a request, I still don't have a solution.

jay123456
  • 131
  • 1
  • 1
  • 10