gpt4 book ai didi

ios - 使用 Swift 从 Firebase 接收信息期间变量不是 "saving"

转载 作者:行者123 更新时间:2023-11-30 12:28:16 26 4
gpt4 key购买 nike

当我尝试从 Firebase 服务器获取信息时,我会将数据加载到某些变量中,以便稍后访问它们。看来我只能访问 Swift 函数中的变量数据,它从 Firebase 数据库获取数据。下面我有一个打印函数,该打印函数打印正确的数据,但是当我稍后尝试在代码中打印该数据时,它不会打印任何内容。

(我使用的是 Swift 3.0 和 Xcode 版本 8.1)

我针对该特定地点的代码:

func loadAllDataFromServerAboutGame(){

ref.child("\(gamePIN)").observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
self.player1Name = value?["player1Name"] as? String ?? ""
self.player2Name = value?["player2Name"] as? String ?? ""
self.player3Name = value?["player3Name"] as? String ?? ""
self.player4Name = value?["player4Name"] as? String ?? ""
self.player5Name = value?["player5Name"] as? String ?? ""
self.player6Name = value?["player6Name"] as? String ?? ""

self.playerAmountBeforeTextChanging = value?["numberOfPlayers"] as? String ?? ""

self.player1UID = value?["player1UID"] as? String ?? ""
self.player2UID = value?["player2UID"] as? String ?? ""
self.player3UID = value?["player3UID"] as? String ?? ""
self.player4UID = value?["player4UID"] as? String ?? ""
self.player5UID = value?["player5UID"] as? String ?? ""
self.player6UID = value?["player6UID"] as? String ?? ""

if let myNumber = NumberFormatter().number(from: self.playerAmountBeforeTextChanging) {
self.numberOFPLayers = myNumber.intValue
} else {
print("Error converting text into 'int' value!")
}

print("Transcription: \(self.numberOFPLayers)")
print("Player 3 Name: \(self.player3Name)")

// ...
}) { (error) in
print("DataBase ERROR:\(error.localizedDescription)")
}

}

提前致谢!

最佳答案

您必须记住,observeSingleEvent 是异步触发的,因此将其放入调度组可能是一个好主意。

我建议您做的是从组内执行方法loadAllDataFromServerAboutGame,并且在它完成触发并且所有数据都已写入变量之前不要执行任何操作。

调用 loadAllDataFromServerAboutGame 的位置

let group = DispatchGroup()
group.enter()
loadAllDataFromServerAboutGame()

group.notify(queue: .main, execute: {
// Do something
}

您的loadAllDataFromServerAboutGame方法:

func loadAllDataFromServerAboutGame(){

ref.child("\(gamePIN)").observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
self.player1Name = value?["player1Name"] as? String ?? ""
self.player2Name = value?["player2Name"] as? String ?? ""
self.player3Name = value?["player3Name"] as? String ?? ""
self.player4Name = value?["player4Name"] as? String ?? ""
self.player5Name = value?["player5Name"] as? String ?? ""
self.player6Name = value?["player6Name"] as? String ?? ""

self.playerAmountBeforeTextChanging = value?["numberOfPlayers"] as? String ?? ""

self.player1UID = value?["player1UID"] as? String ?? ""
self.player2UID = value?["player2UID"] as? String ?? ""
self.player3UID = value?["player3UID"] as? String ?? ""
self.player4UID = value?["player4UID"] as? String ?? ""
self.player5UID = value?["player5UID"] as? String ?? ""
self.player6UID = value?["player6UID"] as? String ?? ""

if let myNumber = NumberFormatter().number(from: self.playerAmountBeforeTextChanging) {
self.numberOFPLayers = myNumber.intValue
} else {
print("Error converting text into 'int' value!")
}

print("Transcription: \(self.numberOFPLayers)")
print("Player 3 Name: \(self.player3Name)")
//HERE:
group.leave()

// ...
}) { (error) in
print("DataBase ERROR:\(error.localizedDescription)")
//HERE:
group.leave()
}
}

关于ios - 使用 Swift 从 Firebase 接收信息期间变量不是 "saving",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43905023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com