问题
I need to handle "Permission Denied" error differently from all other errors.
In Android I've done it: (Kotlin)
override fun onCancelled(error: DatabaseError) {
if (error.code == DatabaseError.PERMISSION_DENIED) {
// Warn user
}
}
In iOS I have only:
someDbReference.observe(.childAdded, with: { data in
// Do something
}, withCancel: { err in
// err is Error
print((err as NSError).code) // 1
// Which error? O_o
})
How to determine error type? I've read whole documentation, and I think that err.localizedDescription == "Permission Denied"
is a really bad way.
回答1:
Just like it was already mentioned in the comments, I use same approach to determine type of error Firebase returns. Eg:
someDbReference.observe(.childAdded, with: { data in
// Do something
}, withCancel: { (returnedError: Error) in
// Casting Error to NSError
let testError: NSError = searchError as NSError
// This is just random error from FB documentation, you can find all there
if testError.code == FIRAuthErrorCode.errorCodeUserDisabled.rawValue {
// User disabled
}
})
Make sure you cast returned error object which is type of Error to NSError. Use its error code to compare it to Firebase error code.
来源:https://stackoverflow.com/questions/45140666/how-to-handle-firebase-database-error-in-ios-askfirebase