异步函数未由完成处理程序处理

时间:2018-08-06 05:42:47

标签: ios asynchronous swift4 google-distancematrix-api

我正在使用google maps距离矩阵api,并使用了compeletion处理程序来传递我的异步函数调用,如下所示:

func configureRoute(origin:String,destination:String, completionHandler: @escaping (_ duration:Int) -> ()){
    let jsonURL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=place_id:\(origin)&destinations=place_id:\(destination)&key=MYAPI"
    guard let url = URL(string: jsonURL ) else {return}
    print(jsonURL)

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else {return}
        do {
            let route = try JSONDecoder().decode(Welcome.self, from: data)
            // print(self.durations)
            completionHandler(route.rows[0].elements[0].duration.value)
        }
        catch let jsonErr {
        }
        let dataAsString = String(data: data, encoding: .utf8)
    }.resume()
}

,然后我尝试像这样使用该结果,但是我的输出结果仍为空白,并且无法使用已收到的任何呼叫。我对强制处理程序不是很好,所以如果有人能让我知道我做错了什么?

func majorConfigure() {
    permutations(placeID.count, &placeID, origin: startPlaceID, destination: endPlaceID)
    for eachArray in finalRoutes {
        for i in 0..<(eachArray.count-1) {
            configureRoute(origin: eachArray[i], destination: eachArray[i+1]){
                duration in
                self.durations.append(duration)
            }
        }
        groupedDurations.append(durations)
        durations.removeAll()
    }
    print(groupedDurations)
}

使用调度组后,这是我的更新代码:

 func majorConfigure(){
    permutations(placeID.count, &placeID, origin: startPlaceID, destination: endPlaceID)
    let dispatchGroup = DispatchGroup()
    for eachArray in finalRoutes{
        for i in 0..<(eachArray.count-1){
            dispatchGroup.enter()
            configureRoute(origin: eachArray[i], destination: eachArray[i+1]){
                duration in
                self.durations.append(duration)
                print(self.groupedDurations)
                dispatchGroup.leave()
            }
        }
        dispatchGroup.notify(queue: .main){
            self.groupedDurations.append(self.durations)
            self.durations.removeAll()
        }
    }

 }

我打印groupedDurations的结果是:

[]
[]
[]
[]
[]
[]

1 个答案:

答案 0 :(得分:0)

尝试一下:

    func majorConfigure() {
permutations(placeID.count, &placeID, origin: startPlaceID, destination: endPlaceID)
for eachArray in finalRoutes {
    for i in 0..<(eachArray.count-1) {
        configureRoute(origin: eachArray[i], destination: eachArray[i+1]){
            duration in
            self.durations.append(duration)
               if i == eachArray.count - 1{
                groupedDurations.append(durations)
               }
            if i == eachArray.count - 1 && eachArray == finalRoutes.last! {
                 print(groupedDurations)
                 durations.removeAll()
              } 
        }
    }


}

}

编辑后的答案

您可以通过这种方式实现:

var indexFinalRoutes = 0 
var indexEachArray = 0

func loopFinalRoutes(){
    self.indexEachArray = 0
    let eachArray = finalRoutes[indexFinalRoutes]
    loopEachArray(eachArray: eachArray) { (success) in
        self.indexFinalRoutes = self.indexFinalRoutes + 1
        if self.indexFinalRoutes < self.finalRoutes.count - 1{
            self.loopFinalRoutes()
        }else{
            self.indexFinalRoutes = 0
            print(self.groupedDurations)
        }
    }
}

func loopEachArray(eachArray : [String] ,
                   completion: @escaping (_ success:Bool) -> Void){

    if indexEachArray + 1 < eachArray.count - 1 {
        configureRoute(origin: eachArray[indexEachArray], destination: eachArray[indexEachArray+1]){
            duration in
            self.durations.append(duration)
            self.indexEachArray = self.indexEachArray + 1
            let nextEachArray = self.finalRoutes[self.indexFinalRoutes]
            self.loopEachArray(eachArray: nextEachArray, completion: completion)
        }
    }else{
        groupedDurations.append(durations)
        durations.removeAll()
        completion(true)
    }

}

从majorConfigure函数调用loopFinalRoutes functuin,您将获得输出。