gpt4 book ai didi

swift - 使用异步方法的 While 循环

转载 作者:可可西里 更新时间:2023-11-01 01:18:36 26 4
gpt4 key购买 nike

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

循环:

                           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)

}

最佳答案

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

关于swift - 使用异步方法的 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com