Swift Firebase 在闭包内发送数据

标签 swift firebase swift3 firebase-realtime-database swift2

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

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)")
    }
}

这是我的第二个 View Controller 的代码:

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(_:with:) 是异步的,segue 将同步调用,因此当您在 observe 的完成 block 中获得响应时,您的 segue 已经是已执行。

要解决这个问题,您需要做的是在调用performSegue之前调用observe,并在observe的完成 block 内调用获取响应,使用您要传递的值调用 perfromSegue

关于Swift Firebase 在闭包内发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43889755/

相关文章:

Swift:无法将类型 'GameScene' 的值转换为预期的参数类型 'UIViewController!'

swift - 打字时扩大 uitableview 单元格高度并避免键盘

swift - 在 Swift 中有选择地将类的属性传递给类方法

swift - Ubuntu 14.04 中出现 "swift build"错误

firebase - 连接 Firebase 简单登录和外部服务器

android - Firebase SignedURL 不等于下载 url 存储

ios - swift 同时对同一单元格 ID 使用两个不同的 TableView ,indexPath.row 超出范围

仅适用于沙盒的 iOS Firebase 推送通知

ios - 如何避免调用 viewDidLoad() 刷新 View

ios - 将 Siesta 与非 Restful API 结合使用