1

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)

            }

1 Answer 1

2

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()
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried, but method are completed before async task have been finished, so I don't get values from async task.
This code is working. If it does not work for you, show your code to be able to help you.
I updated my question with updated code, I got printed empty array, but after that I got printed values from block which is inside async task.
Properly intent your brackets, so you can see what is called where. Move dispatchGroup.leave() below the if rulesNumber == rulesCount ... closure. Remove dispatchGroup.wait.Move rulesNumber += 1 as last command inside the repeat closure. Learn how to avoid "Pyramid of doom-", or "spaghetti" code. Try to actually understand what you are doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.