while循环使用异步方法

时间:2017-07-12 06:46:24

标签: swift asynchronous grand-central-dispatch

我遇到了问题。我得到了带有异步方法的while循环的方法。问题是在异步任务完成之前循环完成,所以我的方法完成了几次。因此,当完成所有while循环并且所有异步任务都已完成时,我需要只调用一次complete()。有人可以帮助我,我找到了一些答案,但没有任何帮助解决我的问题。

循环:

                           let dispatchGroup = DispatchGroup()

            repeat {

                dispatchGroup.enter()

                var configSection = "@rule[\(rulesNumber)]"
                print(rulesNumber, rulesCount)


                Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configEnabledOption) { (response1) in
                    MethodsClass().getJsonValue(response_data: response1) { (enabledValue) in
                        Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configAnalogType) { (response2) in
                            MethodsClass().getJsonValue(response_data: response2) { (analogTypeValue) in
                                Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configTypeOption) { (response3) in
                                    MethodsClass().getJsonValue(response_data: response3) { (typeValue) in
                                        Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configTriggerOption) { (response4) in
                                            MethodsClass().getJsonValue(response_data: response4) { (triggerValue) in
                                                Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configActionOption) { (response5) in
                                                    MethodsClass().getJsonValue(response_data: response5) { (actionValue) in
                                                        Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configMinOption) { (response6) in
                                                            MethodsClass().getJsonValue(response_data: response6) { (minValue) in
                                                                Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configMaxOption) { (response7) in
                                                                    MethodsClass().getJsonValue(response_data: response7) { (maxValue) in
                                                                        Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configMinCOption) { (response8) in
                                                                            MethodsClass().getJsonValue(response_data: response8) { (minCValue) in
                                                                                Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configMaxCOption) { (response9) in
                                                                                    MethodsClass().getJsonValue(response_data: response9) { (maxCValue) in
                                                                                        Json().deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configOutPut) { (response10) in
                                                                                            MethodsClass().getJsonValue(response_data: response10) { (outPutNb) in




                                                                                                if (!enabledValue.isEmpty && !typeValue.isEmpty && !triggerValue.isEmpty && !actionValue.isEmpty) {
                                                                                                    object["Enabled"] = enabledValue
                                                                                                    object["Type"] = typeValue
                                                                                                    object["Trigger"] = triggerValue
                                                                                                    object["Action"] = actionValue

                                                                                                    object["AnalogType"] = analogTypeValue
                                                                                                    object["MinValue"] = minValue
                                                                                                    object["MaxValue"] = maxValue
                                                                                                    object["MinCValue"] = minCValue
                                                                                                    object["MaxCValue"] = maxCValue
                                                                                                    object["OutputNb"] = outPutNb
                                                                                                    arrayObjects.append(object)

                                                                                                }

                                                                                                if rulesNumber == rulesCount {
                                                                                                    print(rulesNumber, rulesCount, "f", arrayObjects)
                                                                                                   // complete(arrayObjects)
                                                                                                }

                                                                                            }}
                                                                                    }} }}}}  }}}}}}}}}}
                    }}
                rulesNumber += 1



                dispatchGroup.leave()

            dispatchGroup.wait(timeout: DispatchTime.distantFuture)
            } while (rulesNumber < rulesCount)

            dispatchGroup.notify(queue: DispatchQueue.main) {
                print(arrayObjects)
               complete(arrayObjects)

            }

1 个答案:

答案 0 :(得分:2)

complete()移到循环之外:

let dispatchGroup = DispatchGroup()
repeat {
    // ...
    dispatchGroup.enter()
    deviceinform(token: token as! String, config: iomanConfig, section: configSection, option: configEnabledOption) { (response1) in
        // ...
        dispatchGroup.leave()
    }
}

dispatchGroup.notify(queue: DispatchQueue.main) {
    complete()
}
相关问题