使用 Swift 链式函数的完成处理程序

Working with Swift completion handlers for chained functions

我正在将一些函数链接在一起,我无法弄清楚如何在所有函数完成后调用具有 return 值的完成处理程序 运行ning.

class AirQualityProvider {
    var aBlock: ((Int?) -> Void)?

    func getAirQuality(completion: @escaping (Int?) -> Void) {
        aBlock = completion
        callAPI()
    }

    private func callAPI() {
        let data = Data()
        parseDataForAQI(data: data)
    }

    private func parseDataForAQI(data: Data) {

        for d in data {
            dosomeMath(d)
        }
    }


    private func dosomeMath(data: Int) {

        // HERE IS WHERE I WANT IT TO SUM UP ALL THE NUMBERS
        THEN ONLY RETURN ONE VALUE using a completion handler.
        Currently, it returns the average as it is being generated.
    }

在 Alexander 的帮助下几乎成功了。 Alexander 提供的代码运行完美,令人惊叹。问题是,当我在 alamofire 中 运行 task运行ner 时,它 return 是空的。在 alamofire 之外,它照常工作。我需要在 alamofire 中 运行 这个。

func A(json : JSON){
    for (key,subJson) in json{
        if subJson["free"].doubleValue > 0.0 {
            func B(asset: subJson["asset"].stringValue, json: subJson)
        }
    }

    print(taskRunner.getResults())
}


func B(asset : String, json : JSON){

        //OUTSIDE ALAMOFIRE WORKS
            self.taskRunner.execute{
            return 100
        }

    Alamofire.request(url).responseJSON { response in

       //INSIDE ALAMOFIRE DOESN'T WORK. Returns []
            self.taskRunner.execute{
            return 100
        }

    }

}

我会使用调度队列来同步结果的聚合(通过同步 Array.append(_:) 调用,以及随后的数组读取)。这是一个简单的例子:

import Dispatch
import Foundation

class ParallelTaskRunner<Result> {
    private var results = [Result]()
    private let group = DispatchGroup()
    private let resultAggregatorQueue = DispatchQueue(label: "Result Aggregator")

    func execute(_ closure: (@escaping (Result) -> Void) -> Void) {
        group.enter() // Register that a new task is in-flight
        closure { result in
            self.resultAggregatorQueue.sync { // Synchronize access to the array
                self.results.append(result) // Record the result
            }
            self.group.leave() // This task is done
        }
    }

    func getResults() -> [Result] {
        group.wait() // Make sure all in-flight tasks are done
        return resultAggregatorQueue.sync { return results }
    }
}




let taskQueue = DispatchQueue(label: "Task Queue", attributes: .concurrent)
let taskRunner = ParallelTaskRunner<Int>()
for i in 0...100 {
    taskRunner.execute { completionHandler in 
        taskQueue.async { // Simulated async computation
            let randomTime = 3.0
            print("Sleeping for \(randomTime)")
            Thread.sleep(forTimeInterval: randomTime) // Simulates intesnive computation
            let result = i // Simulate a result
            completionHandler(result)
        }
    }
}
print(taskRunner.getResults()) // Oh look, all the results are here! :D