【问题标题】:How to solve this closure completion handler issue?如何解决这个闭包完成处理程序问题?
【发布时间】:2021-05-07 02:34:47
【问题描述】:

我已经死盯着这些功能近两天了,想知道为什么我不能得到我想要的结果。

这是第一个函数:

   func getTheSchoolID(completion: @escaping ((String?)-> ())) {
    db.collection("school_users").whereField("userID", isEqualTo: user!.uid).getDocuments { (querySnapshot, err) in
        if let err = err {
            print("There was an error fetching the documents: \(err)")

        } else {
            self.skoolID = querySnapshot!.documents.map { document in
                return SchoolID(schoolID: (document.get("school_id") as! String))
            }
        }
    }

    let fixedID = "\(skoolID)"
    let substrings = fixedID.dropFirst(28).dropLast(3)
    let realString = String(substrings)
    completion(realString)

}

这是我上一个 StackOverflow 问题的答案中的函数,所以我使用它并理解了闭包完成处理程序的概念。

下一段代码是我想要发生但没有发生的。

    getTheSchoolID { [weak self] (realString) in
        if let id = realString {
            DispatchQueue.main.async {
                self?.schoolIDTextF.text = id

            }
        }
    }

现在,当我检查 Viewcontroller 中的文本字段时,它仍然是空的。

集合路径很好,字段正确我什至会添加我的 Firestore 数据库的屏幕截图以供参考。

如果有人可以提供帮助,那就太好了。如果你给我答案并且它有效,我什至会发送 10 美元的 Paypal 付款。这就是它对我的意义。谢谢。

【问题讨论】:

    标签: swift google-cloud-platform google-cloud-firestore closures completionhandler


    【解决方案1】:

    getDocuments 异步工作,你必须调用completion inside闭包

    func getTheSchoolID(completion: @escaping (String?) -> Void) {
        db.collection("school_users").whereField("userID", isEqualTo: user!.uid).getDocuments { (querySnapshot, err) in
            if let err = err {
                print("There was an error fetching the documents: \(err)")
                completion(nil)
            } else {
                self.skoolID = querySnapshot!.documents.map { document in
                    return SchoolID(schoolID: document.get("school_id") as! String)
                }
                let fixedID = "\(self.skoolID)"
                let substrings = fixedID.dropFirst(28).dropLast(3)
                let realString = String(substrings)
                completion(realString)
            }
        }
    }
    

    而且你使用的括号太多了。

    map 表达式看起来很奇怪。

    【讨论】:

    • 非常感谢它有效。你还想要10美元吗? @vadian
    【解决方案2】:

    您过早地宣布关闭。您需要在 db...getDocuments 闭包内调用它,例如:

    func getTheSchoolID(completion: @escaping ((String?)-> ())) {
        db.collection("school_users").whereField("userID", isEqualTo: user!.uid).getDocuments { (querySnapshot, err) in
            if let err = err {
                print("There was an error fetching the documents: \(err)")
    
            } else {
                self.skoolID = querySnapshot!.documents.map { document in
                    return SchoolID(schoolID: (document.get("school_id") as! String))
                }
                let fixedID = "\(skoolID)"
                let substrings = fixedID.dropFirst(28).dropLast(3)
                let realString = String(substrings)
                completion(realString)
            }
        }
    }
    

    留着钱买一本好书 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2020-05-13
      相关资源
      最近更新 更多