从Firebase检索数据的问题

时间:2019-02-15 00:25:29

标签: swift firebase firebase-realtime-database swift3

我一直在使用Firebase开发在线实时游戏。我有多个玩家在地图上移动一个圆形节点,将其当前位置点发送并检索到Firebase数据库中。但是,它相当缓慢,缓慢,并且似乎没有足够快地从数据库上传。

这只是我的替代功能更新中的代码片段。每个玩家控制2个圆圈的位置,并将位置点发送到数据库。每个玩家检索对方的位置点并进行更新。

override func update(_ currentTime: TimeInterval) {
    let dict1 = ["x": circle1PositionX, "y": circle1PositionY] as [String: Any] let dict2 = ["x": circle2PositionX, "y": circle2PositionY] as [String: Any]

    guard let players = self.userData?.mutableArrayValue(forKey: "players"), let currentPlayer =  self.userData?.value(forKey: "currentPlayer"), let gameID = self.userData?.value(forKey: "gameID") else {
        print("Something is missing")
        return
    }
                let playersArray = players as Array


    if (currentPlayer as? String == playersArray[1] as? String) {
        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[1] as! String).child("circle").setValue(dict2)
        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[1] as! String).child("circleB").setValue(dict2B)

        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[0] as! String).child("circle").observeSingleEvent(of: .value, with: { (snapshot) in
            guard let okx = snapshot.childSnapshot(forPath: "x").value as? Double else {
                return
            }
            guard let oky = snapshot.childSnapshot(forPath: "y").value as? Double else {
                return
            }

            self.circle1.position = CGPoint(x: okx, y: oky)

        })

    }

    else if (currentPlayer as? String == playersArray[0] as? String) {

        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[0] as! String).child("circle").setValue(dict1)
        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[0] as! String).child("circleB").setValue(dict1B)


        GameDataSource.ref.child("locations").child(gameID as! String).child(playersArray[1] as! String).child("circle").observeSingleEvent(of: .value, with: { (snapshot) in
            guard let okx = snapshot.childSnapshot(forPath: "x").value as? Double else {
                return
            }
            guard let oky = snapshot.childSnapshot(forPath: "y").value as? Double else {
                return
            }

            self.circle2.position = CGPoint(x: okx, y: oky)

        })
    }

0 个答案:

没有答案