For*_*cke 4 ios firebase swift firebase-realtime-database
我有一些函数可以从Firebase读取数据,但有时候我从来没有得到过响应(或者它被大量延迟).我在这里读到,Firebase可能会在收到数据之前关闭套接字连接.看起来这里有人有类似的问题,但从未发布过解决方案.
以下是我从Firebase下载用户数据的代码示例.
// loads the current user's information
static func loadUserDataWithCompletion(completion: (UserInfo) -> Void) {
let ref = FIRDatabase.database().reference()
print("loading current user data...")
let uid = (FIRAuth.auth()?.currentUser?.uid)!
ref.child("users").queryOrderedByKey().queryEqualToValue(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
print("found user data!")
if let dictionary = snapshot.value as? [String:AnyObject] {
let info = userFromDict(dictionary)
// execute code slated for completion
completion(info)
}
})
}
Run Code Online (Sandbox Code Playgroud)
有什么方法我可以使用observeEventType?也许那时我至少会获得有关问题发生原因的更多信息.
在观察数据库时,您可能会遇到三种可能的错误: -
observe查询的正确路径对于前两个你必须自己照顾它,但对于第三个条件,你可以使用withCancel块: -
FIRDatabase.database().reference().child("users").queryOrderedByKey().queryEqual(toValue: "uid").observe(.childAdded, with: { (snapshot) in
//your code
}, withCancel: {(err) in
print(err) //The cancelBlock will be called if you will no longer receive new events due to no longer having permission.
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1536 次 |
| 最近记录: |