How to handle Firebase Database error in iOS? #AskFirebase

守給你的承諾、 提交于 2021-02-07 19:50:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!