变量不"保存"在使用Swift从Firebase接收信息时

时间:2017-05-11 01:27:19

标签: ios swift firebase firebase-realtime-database

当我尝试从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)")
        }

    }

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须记住observeSingleEvent是异步触发的,因此将它放入Dispatch Group可能是个好主意。

我建议你做的是从组内执行方法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()
    }
}