ios - ObsetveSingleEvent 错误处理
问题描述
userRef.child(userId).observeSingleEvent(of: .value, with: { snapshot in
//some codes
})
这是我从 firebase 数据库中获取一些用户数据的代码。我的问题是如果出现错误(如网络错误、请求超时、未知错误)如何获取错误?与其他事件(setValue、updateValue 等)相比,observeSingleEvent 没有完成块
我也试过:
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
}
推荐阅读
- logstash - 我无法在 logstash 中为 activamq 日志创建 grok 模式
- c# - 如何使用 C# 使用相同的 SQL Server 连接运行 foreach 循环
- javascript - 使用 chai 进行单元测试验证
- apache-kafka - 使用 ConfluentSchemaRegistry 反序列化 avro 数据时出现异常?
- django - 从 postgres 检索数据时如何避免使用`extra`?
- javascript - 如何计算直到最后一个动作发生的动作持续时间(Javascript)
- python - Python创建一个输出多个项目的生成器
- html - 页面顶部的自定义警报
- python-3.x - 为什么我在 Python 中向元组添加内容时必须使用“,”?
- unix - 除空行外,用单引号将每一行括起来