如何更改触发功能的顺序? [英] How can I change the order of functions triggered?

查看:42
本文介绍了如何更改触发功能的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Swift编码,对我的代码有疑问.在下面的代码中,我试图首先获取表视图单元格的信息,然后执行segue.我还使用Firestore保存数据.问题是当我使用打印时,我可以先看到 segue触发!!! ,然后看到文档已保存!.由于我想将 doc.documentID 的值传递给下一个视图控制器,因此我想在触发执行segue之前保存documentID .....

I'm working on Swift coding and have question in my code. in the code below, I'm trying to get the information of the table view cell first and then perform segue. I'm also using Firestore to save the data. The problem is when I use print, I can see segue triggered!!! first and then document saved!!. Since I want to pass the value of doc.documentID, to the next view controller, I want to save the documentID before the perform segue is triggered.....

class HomeViewController: UIViewController {
    var gameDocumentID = ""
// more codes here...
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == K.homeToGameScreen {
            let gameScreenVC = segue.destination as! GameScreenViewController

                gameScreenVC.gameDocumentID = gameDocumentID
        }
    }
}


        extension HomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // serch game db where player1 is ready to play
    
    db.collection(K.FStore.newGameCpllection).whereField(K.FStore.uID, isEqualTo: players[indexPath.row].uID).addSnapshotListener { (querySnapshot, err) in
        if let err = err {
            print("Error getting game db: \(err)")
        } else {
            
            for doc in querySnapshot!.documents {
                
                print("document saved!!")
                self.gameDocumentID = doc.documentID
            
            self.db.collection(K.FStore.newGameCpllection).document(self.gameDocumentID).updateData([
                K.FStore.player2Field: self.playerInfo[K.FStore.nameField]!
            ]) { err in
                if let err = err {
                    print("Error updating document: \(err)")
                } else {
                    print("Document successfully updated")
                    
                }
                print("segue triggered!!!")
                self.performSegue(withIdentifier: K.homeToGameScreen, sender: self)
                }
            }
            }
        
        }

    }
}

推荐答案

数据是从Firebase异步加载的.由于那可能要花一些时间,因此完成处理程序的调用要比您预期的要晚.

Data is loaded from Firebase asynchronously. Since that may take some time, your completion handler is called later than you might expect.

由于这个原因,任何需要数据库中数据的代码都必须位于完成处理程序内部,或者从那里被调用.

For this reason, any code that needs the data from the database, needs to be inside the completion handler, or be called from there.

所以最简单的解决方法是将 performSegue 移至回调中:

So the simplest fix is to move the performSegue into the callback:

extension HomeViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    db.collection(K.FStore.newGameCpllection)
      .whereField(K.FStore.uID, isEqualTo:players[indexPath.row].uID)
      .addSnapshotListener { (querySnapshot, err) in
            if let err = err {
                print("Error getting game db: \(err)")
            } else {
                for doc in querySnapshot!.documents {
            
                print("document saved!!")
                self.gameDocumentID = doc.documentID                
                self.db.collection(K.FStore.newGameCpllection).document(self.gameDocumentID).updateData([
                    K.FStore.player2Field: self.playerInfo[K.FStore.nameField]!
                ]) { err in
                    if let err = err {
                        print("Error updating document: \(err)")
                    } else {
                        print("Document successfully updated")
                    }
                    print("segue triggered!!!")
                    self.performSegue(withIdentifier: K.homeToGameScreen, sender: self)
                }
            }
            
        }
    }
    }
}

另请参阅:

  • Is Firebase getDocument run line by line?
  • Handling asynchronous Firestore data reading in tableView - iOS
  • Storing asynchronous Cloud Firestore query results in Swift, using a dispatch group to seemingly change the order of execution.
  • How do I save data from cloud firestore to a variable in swift?

这篇关于如何更改触发功能的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆