Egle Matutyte
Egle Matutyte

Reputation: 225

While loop with async methods

I got problem. I got method which got while loop with async methods inside. And problem is that while loop finish before async task are finished, so my methods finish a few times. So I need that complete() would be called only once, when all while loop are done and all async task are already finished. Can somebody help me, I found a few answers, but nothing helped with my problem.

Loop:

                           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)

            }

Upvotes: 1

Views: 643

Answers (1)

shallowThought
shallowThought

Reputation: 19602

Move complete() outside the loop:

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()
}

Upvotes: 2

Related Questions