首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >访问Firebase中的用户帐户时生成完成阻止错误

访问Firebase中的用户帐户时生成完成阻止错误
EN

Stack Overflow用户
提问于 2020-09-14 11:30:12
回答 1查看 39关注 0票数 0

我正在尝试创建完成块,同时从firebase表中拉取用户配置文件。在我允许它传回值之前,我需要它完成。

这是我到目前为止所知道的:

代码语言:javascript
运行
复制
func getProf(email: String, pass: String, completionBlock: @escaping (_ success: Bool) -> (Int)) {
        let ref = Database.database().reference()
        let userID = Auth.auth().currentUser?.uid
        ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            self.zDaily = value?["qday"] as? Int ?? 0
        }) {
            if let error = error {
                completionBlock(false)
            } else {
                completionBlock(true)
                return zDaily
            }
        }
        
    }

我得到以下错误:

代码语言:javascript
运行
复制
Cannot convert value of type '() -> _' to expected argument type '((Error) -> Void)?'

我不确定如何解决这个问题,任何建议都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-14 11:36:40

我不确定这是否能修复这个错误。如果没有,那么我想我知道问题出在哪里了,那就是你的错误块和你的observeEvent。

编辑:刚做了一个更改,从observeEvent返回一个错误对象。

代码语言:javascript
运行
复制
 func getProf(email: String, pass: String, completionBlock: @escaping (Bool, Int) -> ()) {
            let ref = Database.database().reference()
            let userID = Auth.auth().currentUser?.uid
            ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
                let value = snapshot.value as? NSDictionary
                self.zDaily = value?["qday"] as? Int ?? 0
            }) { (error) in //Added return error value - this may fix your error
                if let error = error {
                    completionBlock(false, 0) //Add default 0 to return if error
                } else {
                    completionBlock(true, zDaily) //Added zDaily as Int to return
                    //return zDaily - return will not return in async function so you can return the value in completionBlock above.
                }
            }
            
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63877720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档