从Firebase数据库查询数据时的奇怪行为

时间:2019-07-05 08:03:52

标签: swift firebase firebase-realtime-database

我正在尝试在Firebase数据库中写入一些数据,但是我得到了错误的运行过程。有人可以帮我吗?非常感谢!!

JSON树结构如下:

{ 
    "cars": {

        "auto id": [

            "parts": "wheel"
             ...
    .
    .
    .



@objc func buttonTapped(){

    print("before running")
        var ref = Database.database().reference(withPath: "cars").queryOrdered(byChild: "parts").queryEqual(toValue: "wheel")
            ref.observeSingleEvent(of: .childAdded, with: { (snapshot) in
            autoId = snapshot.key
            print("running")
        }) { (error) in
            print(error.localizedDescription)
        }
print("after running")
let newPlanetRef = Database.database().reference().child("company").childByAutoId()
                    newPlanetRef.updateChildValues(autoId)
}

我希望运行的过程是

  

“正在运行”->“正在运行”->“正在运行”

但是实际过程是

  

“正在运行”->“正在运行”->“正在运行”   除非我将更新功能放在闭包内,否则数据无法更新到数据库

1 个答案:

答案 0 :(得分:3)

这是一个异步过程。

您应该这样使用:

'{"en":"Goodbye","fr":"Bonjour"}'

但是我认为这样使用:

  • 为您的参考键创建私有枚举:

    var newPlanetRef: String? //It should be your reference type
    
    var ref = Database.database().reference(withPath: "cars").queryOrdered(byChild: "parts").queryEqual(toValue: "wheel")
            ref.observeSingleEvent(of: .childAdded, with: { (snapshot) in
            autoId = snapshot.key
            newPlanetRef = Database.database().reference().child("company").childByAutoId()
            newPlanetRef.updateChildValues(autoId)
            print("running")
        }) { (error) in
            print(error.localizedDescription)
        }
    
  • 创建数据库引用的全局变量:

    private enum ReferenceKeys {
        static let carsKey = "cars"
    }
    
  • 像这样使用您的函数:

    var database = Database.database()
    var databaseReference = database.reference()
    var carReference = database.reference(withPath: ReferencesKeys.carsKey)
    

奖金:

您可以使用dispatchGroup(或信号量)来等待快照值以继续:

DispatchGroup

var newPlanetRef: String?

var reference = carReference.queryOrdered(byChild: "parts").queryEqual(toValue: "wheel")

reference.observeSingleEvent(of: .childAdded, with: { [weak self] snapshot in

    self?.autoId = snapshot.key
    newPlanetRef = databaseReference.child("company").childByAutoId()
    newPlanetRef.updateChildValues(autoId)

    print("running")
}) { error in 

    print(error.localizedDescription)
}

DispatchSemaphore

@objc func buttonTapped(){

    let dispatchGroup = DispatchGroup()

    print("before running")
    var ref = Database.database().reference(withPath: "cars").queryOrdered(byChild: "parts").queryEqual(toValue: "wheel")

    dispatchGroup.enter()
    ref.observeSingleEvent(of: .childAdded, with: { [weak self]snapshot in
            self?.autoId = snapshot.key

            dispatchGroup.leave()
            print("running")
    }) { (error) in
            print(error.localizedDescription)
    }
    print("after running")
    let newPlanetRef = Database.database().reference().child("company").childByAutoId()
    newPlanetRef.updateChildValues(autoId)
}

完成

@objc func buttonTapped(){

    let dispatchSemaphore = DispatchSemaphore(value: 1)

    print("before running")
    var ref = Database.database().reference(withPath: "cars").queryOrdered(byChild: "parts").queryEqual(toValue: "wheel")

    ref.observeSingleEvent(of: .childAdded, with: { [weak self]snapshot in
            self?.autoId = snapshot.key
            semaphore.signal()

            print("running")
    }) { (error) in
            print(error.localizedDescription)
    }

    semaphore.wait()

    print("after running")
    let newPlanetRef = Database.database().reference().child("company").childByAutoId()
    newPlanetRef.updateChildValues(autoId)
}