Swift Make For Loop等待异步函数结束

时间:2016-06-28 23:55:42

标签: ios xcode swift

我试着使用firebase,我创建了这个函数,它将“Offers”保存到一个Array,然后将这个数组发送到另一个viewcontroller,以便在tableView上显示它们。

我发现一个问题是for循环在数据来自服务器之前结束并且firebase的回调即将到来,你可以看到行“fir(self.goodOffer ...)”是结束firebase回调。

也许有人知道如何让for循环等待firebase回调结束?

THX。

 geocodeAddress(totalFromLocation, callback: { (location) in

                if location != nil {
                    fromLocation = location
                    self.geocodeAddress(totalToLocation, callback: { (location) in
                        if location != nil {
                            toLocation = location
                            mainDistance = Int(self.distanceCalculate(fromLocation!, locationTwo: toLocation!))
                            print(mainDistance)

                            REF_BUSSINES_OFFERS.queryOrderedByKey().queryEqualToValue("north").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
                                print("in snap")
                                if let value = snapshot.value {
                                    if self.isAlavator {
                                        if let area = value["north"] {
                                            if let data = area!["withAlavator"]! {
                                                if let bids = data["bids"] as? Int {
                                                    for i in 1..<bids+1 {
                                                        if let bid = data["\(i)"] {
                                                            let bidFromSize = bid!["fromSize"] as! Int
                                                            let bidToSize = bid!["toSize"] as! Int

                                                            let bidFromDistance = bid!["fromDistance"] as! Int
                                                            let bidToDistance = bid!["toDistance"] as! Int

                                                            if mainDistance >= bidFromDistance && mainDistance <= bidToDistance {
                                                                if Int(totalSize) >= bidFromSize && Int(totalSize) <= bidToSize {
                                                                    let price = String(bid!["price"])
                                                                    let name = bid!["name"] as! String
                                                                    let phoneNumber = bid!["phoneNumber"] as! String
                                                                    var logoImageData: NSData!
                                                                    LOGOS_REF.child("\(phoneNumber)").dataWithMaxSize(3 * 1024 * 1024, completion: { (data, error) in
                                                                        if error != nil {
                                                                            logoImageData = UIImagePNGRepresentation(UIImage(named: "company")!)
                                                                        } else {
                                                                            logoImageData = data
                                                                        }
                                                                        let offer = Offer(logoImage: logoImageData, name: name, price: price)
                                                                        self.goodOffers.append(offer)
                                                                        print("fir \(self.goodOffers[0].price)")
                                                                    })
                                                                }
                                                            }
                                                        }
                                                        print(i)
                                                    }//end of for
                                                    print("out")
                                                    //self.changgeViewToTable()
                                                }

1 个答案:

答案 0 :(得分:2)

无法帮助你,因为你的代码难以阅读。使用ObjectMapper可以降低映射这些字段的级别(和工作量)。无论如何,您可以使用dispatch_group来同步各种异步调用:

let group = dispatch_group_create()
for i in 1..<bids+1 {
    // ...
    dispatch_group_enter(group)
    LOGOS_REF.child("\(phoneNumber)").dataWithMaxSize(3 * 1024 * 1024) {
        // ...

        dispatch_group_leave(group)
    }
}    

// Wait for all async calls to complete before proceeding
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)

当异步任务完成时,每个dispatch_group_enter()应与dispatch_group_leave()保持平衡。