Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用Firebase确保我的代码在Swift中有序运行?_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Ios 如何使用Firebase确保我的代码在Swift中有序运行?

Ios 如何使用Firebase确保我的代码在Swift中有序运行?,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我正在制作一个应用程序,其中运行以下代码块: extension LikeOrDislikeViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let uid = Auth.auth().currentUser?.uid var numRows = 0

我正在制作一个应用程序,其中运行以下代码块:

extension LikeOrDislikeViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let uid = Auth.auth().currentUser?.uid
        var numRows = 0

        Database.database().reference().child("Num Liked").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                let numMoviesLiked = ((dictionary["Number of Movies Liked"] as? Int))!

                if numMoviesLiked % 4 == 0 {
                numRows = numMoviesLiked/4
                }
            }
        })
        return numRows
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.backgroundColor = UIColor.red
        tableView.rowHeight = 113
        cell.textLabel?.text = "\(indexPath.row)"

        return cell
    }
}

我需要数据库。数据库。。。要在返回行之前运行的代码。如何才能做到这一点?

数据库操作非常繁重,需要一些时间才能完成。您应该在数据库之后更新表视图。数据库。。。操作已完成

class LikeOrDislikeViewController {
    ...

    lazy var numRows: Int = {
        let uid = Auth.auth().currentUser?.uid
        Database.database().reference().child("Num Liked").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                let numMoviesLiked = ((dictionary["Number of Movies Liked"] as? Int))!

                if numMoviesLiked % 4 == 0 {
                    self.numRows = numMoviesLiked/4
                    self.tableView.reloadData()
                }
            }
        })
        return 0
    }()
}

extension LikeOrDislikeViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numRows
    }

    ...
}

这是一个错误的设置,因为numberOfRowsInSection将被多次调用。加载数据一次,而不是一次又一次。observeSingleEvent的完成处理程序在返回numRows后运行。放置一些日志记录语句,您可以在输出中轻松看到这一点。您必须将所有需要数据的代码放入完成处理程序中,或者传入您自己的自定义完成处理程序并调用它。看,这不起作用。我得到这个错误:变量在它自己的初始值代码中使用更新。但请记住,这只是一个预览,指出它将如何工作,而不是您可以直接在生产代码中使用的代码;