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

时间:2018-07-02 18:44:34

标签: swift chaining completionhandler

我将一些函数链接在一起,当所有函数运行完毕后,我不知道如何用返回值调用完成处理程序。

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.
    }

几乎可以帮助亚历山大。亚历山大提供的代码完美地工作,这真是太神奇了。问题是,当我在alamofire中运行taskrunner时,它返回空。在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
        }

    }

}

1 个答案:

答案 0 :(得分:0)

我将使用调度队列来同步结果的聚合(通过同步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