0

我想在某个视图控制器的文本字段中显示学校 ID,但由于某种原因我不能。我有一个获取当前学校用户的学校 ID 的函数,我可以在 alertVC 中显示它,但不能在常规 VC 中显示。我将添加图像和解释。

首先,我将展示获取当前用户的学校 ID 并将其显示在 alertVC 中的函数的代码。

  func getTheSchoolsID() -> 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.skoolsID = querySnapshot!.documents.map { document in
                return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
            }
            self.tableView.reloadData()
        }
    }
    let fixID = "\(skoolsID)"
    let substring = fixID.dropFirst(28).dropLast(3)
    let realString = String(substring)
    
    return realString
}

这里也是 alertVC 的代码。

@IBAction func infoPressed(_ sender: UIBarButtonItem) {
    var text = UITextField()
    
    let alert = UIAlertController(title: "User Info", message: "School ID", preferredStyle: .alert)
    let action = UIAlertAction(title: "Dismiss", style: .cancel) { (action) in
        self.dismiss(animated: true, completion: nil)
    }
    alert.addTextField { (alertTextField) in
        alertTextField.text = self.getTheSchoolsID()
        alertTextField.isEnabled = false
       
        
        
    }
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
}

这两个产生了这个结果。 积极的结果

这也是文档中的确切 school_id。

现在,当我想在我添加新事件的另一个 VC 的文本字段中显示完全相同的值时,它什么也不显示。

负面结果

该代码与第一个代码片段完全相同,但 reloadData() 除外,这很有意义,并且不会产生相同的结果。

func getTheSchoolID() -> 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 substring = fixedID.dropFirst(28).dropLast(3)
    let realString = String(substring)
    return realString
}

这是我也试图声明它。

   public override func viewDidLoad() {
    super.viewDidLoad()
    
    errLabel.alpha = 0
    
    getTheSchoolID()
    
    
    
    schoolIDTextF.text = getTheSchoolID()
    // Do any additional setup after loading the view.
}

如果有人可以帮助解决这个问题,将不胜感激。我试图尽可能地解释它。我知道有很多信息,但是如果您仍然需要更多信息,我可以在评论中澄清。谢谢。

4

2 回答 2

0

问题在于异步 Firebase 代码。使用闭包或完成处理程序来获取结果。

将您的getSchoolId修改为

    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)")
                completion(nil)
            } else {
                self.skoolID = querySnapshot!.documents.map { document in
                    return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
                }
            }
        }
        
        let fixedID = "\(skoolID)"
        let substring = fixedID.dropFirst(28).dropLast(3)
        let realString = String(substring)
        completion(realString)
    }

在您的 VC 中,将其用作

override func viewDidLoad() {
    super.viewDidLoad()
    
    errLabel.alpha = 0
    
    getTheSchoolID(completion: {
       [weak self] schoolId in
        if let id = schooldId {
          DispatchQueue.main.async {
            self?.schoolIDTextF.text = id 
          }
        }
    })
}
于 2021-02-01T18:42:13.670 回答
0

Firestore 集合中的 GetSchoolIds 是异步任务。所以使用闭包你可以实现这一点。

  func getTheSchoolsID(completion:@escaping(_ schoolIds:[SchoolID]?) -> 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 {
                let schoolsId = querySnapshot!.documents.map { document in
                    return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
                }
                completion(schoolsId)
            }
        }
    }

将此功能用于您的 VC

override func viewDidLoad() {
    super.viewDidLoad()
    
    errLabel.alpha = 0
    
    getTheSchoolsID(completion: {
       [weak self] schoolIds in
        if let schoolIdObj = schoolIds?.first {
        let substring = schoolIdObj.schoolID.dropFirst(28).dropLast(3)
        let realString = String(substring)

          DispatchQueue.main.async {
            self?.schoolIDTextF.text = realString 
          }
        }
    })
}
于 2021-02-10T07:34:39.950 回答