【问题标题】:Firebase Query Error HandlingFirebase 查询错误处理
【发布时间】:2016-10-28 05:44:40
【问题描述】:

我有一些函数可以从 Firebase 读取数据,但有时我从来没有得到响应(或者它被严重延迟)。我读到here,也许 Firebase 可以在接收到数据之前关闭套接字连接。看起来有人遇到了类似的问题here,但从未发布过解决方案。

这是我从 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)
        }
    })
}

有什么方法可以使用observeEventType 检测错误吗?也许那时我至少会得到更多关于为什么会发生问题的信息。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    您在观察数据库时可能会遇到三个可能的错误:-

    • 您尚未定义数据库的正确路径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.
    
        })
    
    • 至于如果用户在写入时失去网络连接的错误处理,无论网络延迟或连接如何,您的应用都会保持响应。请参阅Firebase Docs离线写入数据 部分

    【讨论】:

    • 谢谢,我会研究这些可能性,如果我找到解决办法,请告诉您。
    • 没有,我还没有找到解决办法。 :( 一旦这个项目更加稳定,我将尝试隔离问题并查看是否可以重现它。
    猜你喜欢
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多