如何两次访问异步Firebase

时间:2019-07-23 09:04:35

标签: swift firebase firebase-realtime-database

我尝试两次访问firebase,但是我只能在getId()函数中成功访问,而不能在for循环中访问。 我什至在for循环下设置了一个断点,但是它并没有在关闭时停止。

以下是我的JSON树的结构和示例代码

"cars": {

    "auto id": [

        "parts": 
                {
                 "auto id 1": item 1
                 "auto id 2": item 2
                 "auto id 3": item 3

         ...
.
.
.



   private func getId(path:String, value:String?, resultNode:String ) {

        let ref = Database.database().reference(withPath: path).queryOrdered(byChild: "name").queryEqual(toValue: value)

        ref.observeSingleEvent(of: .childAdded, with: { snapshot in
            BookingViewController.carDict[resultNode] = snapshot.key as AnyObject 
        })
    }
    @objc func onTapped(){

    getId("","","")
    getId("","","")
    getId("","","")

    //After getId, carDict has got all the information
    //Then i need to update part information, but it failed
    for part in self.parts {

        BookingContainerViewController.Id =                  
   xxx.childByAutoId().key
                        let ref = Database.database().reference().child("cars").child(bookingView.id)
                        ref.observeSingleEvent(of: .childAdded, with: { (snapshot) in
                            ref.updateChildValues(BookingViewController.carDict)
                            ref.child(nodeName).childByAutoId().setValue(part)


                        })


}

0 个答案:

没有答案