userRef.child(userId).observeSingleEvent(of: .value, with: { snapshot in
//some codes
})
这是我从 firebase 数据库获取一些用户数据的代码。我的问题是,如果出现错误(如网络错误、请求超时、未知错误),如何获取错误?与其他事件(setValue、updateValue 等)相比,observeSingleEvent 没有completionBlock
我也尝试过:
userRef.child(userId).observeSingleEvent(of: .value, with: {(snapshot) in
// print something
} , withCancel: {(error) in
// print something
})
仍然不会进入取消状态。
最佳答案
这是您要找的吗?
userRef.child(userId).observeSingleEvent(of: .value, with: { (snapshot) in
print("Worked")
}) { (error) in
print("Didn't")
}
您还可以再次检查,以防您的观察出现错误。
if snapshot.value is NSNull{
//snapshot is null
} else{
//Not null
}
关于ios - ObsetveSingleEvent 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51182806/