【问题标题】:Swift Firebase sending data inside a closureSwift Firebase 在闭包内发送数据
【发布时间】:2017-05-10 10:23:27
【问题描述】:

我正在尝试将数据发送到另一个视图控制器。但是,无法在第二个视图控制器处访问数据。这是我的代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)


    switch(segue.identifier ?? "") {

    case "tograddetail":
        print("Going to Grad Detail")


        guard let gradDetailViewController = segue.destination as? graduatedetailViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedgradCell = sender as? GradTableViewCell else {
            fatalError("Unexpected sender: \(sender)")
        }

        guard let indexPath = tableView.indexPath(for: selectedgradCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }


        ref = FIRDatabase.database().reference().child("Database")
        ref.observe(FIRDataEventType.value, with: { (snapshot) in

            //print(snapshot.value)
            if snapshot.exists() {
                if let countdowntime = snapshot.value as? NSDictionary {

                    let selectedgrad = self.graduatename[indexPath.row]

                    if let graddata = countdowntime[selectedgrad] as? NSDictionary {

                        let theinstitution = graddata["Institution"] as! String
                        let thelocation = graddata["location"] as! String
                        let thetimeleft = graddata["timeleft"] as! Int

                        guard let firstgrad = graddetail(institution: theinstitution, location: thelocation, timeleft: thetimeleft) else {
                            fatalError("Unable to instantiate graddetail")
                        }
                        //print(firstgrad.institution)

                        //print(destinationgraddata.grad?.institution)



                        let destinationVC = segue.destination as! graduatedetailViewController
                        destinationVC.grad = firstgrad


                    }

                }
            }
        })

    default:
        fatalError("Unexpected Segue Identifier; \(segue.identifier)")
    }
}

这是我的第二个视图控制器的代码:

var grad: graddetail?

@IBOutlet weak var theinstitution: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    if let grad = grad {

        theinstitution.text = grad.institution

    }
}

但是,grad.institution 值始终返回 nil。有什么想法吗?

【问题讨论】:

  • 因为.observe 是异步的,segue 会调用同步,所以当你在它的完成块中得到响应时,你的 segue 已经执行了
  • 那么我该如何避免这个问题呢?
  • 您需要做的是使用.observe 获取价值并在observe 的完成块中调用perfromSegue
  • 谢谢!问题解决了
  • 是的!来吧

标签: swift firebase swift3 firebase-realtime-database swift2


【解决方案1】:

问题是 observe(_:with:) 是异步的,segue 将同步调用,因此当您在 observe 的完成块中获得响应时,您的 segue 已经执行。

要解决此问题,您需要做的是在调用performSegue 之前调用observe,并在收到响应时在observe 的完成块内调用perfromSegue,并使用您要传递的值.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2017-07-18
    • 1970-01-01
    • 2021-06-25
    • 2020-12-05
    • 2010-12-21
    • 2020-12-27
    相关资源
    最近更新 更多