Swift Firebase在关闭内发送数据

Swift Firebase在关闭内发送数据,swift,firebase,swift3,firebase-realtime-database,swift2,Swift,Firebase,Swift3,Firebase Realtime Database,Swift2,我正在尝试将数据发送到另一个视图控制器。但是,无法在第二个视图控制器上访问数据。这是我的密码: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) switch(segue.identifier ?? "") { case "tograddetail": print("Going t

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

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值始终返回零。有什么想法吗?

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


要解决这个问题,您需要做的是在调用
performSegue
之前调用
observe
,在
observe
的完成块内,当您得到响应时,调用
perfromSegue
,使用您想要传递的值。

因为
observe
是异步的,因此当您在完成块中得到响应您的segue已经执行了,那么如何避免此问题?您需要做的是使用
获取值。观察
并在观察的完成块中调用perfromsegue谢谢!问题的解决是肯定的!着手