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