Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何等待完成处理程序完成?调度组?_Swift_Dispatch Async - Fatal编程技术网

Swift 如何等待完成处理程序完成?调度组?

Swift 如何等待完成处理程序完成?调度组?,swift,dispatch-async,Swift,Dispatch Async,我需要帮助修改代码,以便在循环之前等待完成处理程序启动 问题 我正在尝试运行以下代码块: let sourceCodes:[String] = self.makeCodeArray(codeString: (self.sourceDict?["Code"]!)!) print("A") for i in 0..<sourceCodes.count { print("B") getRealtimeData(stopCode: sourceCodes[i], completi

我需要帮助修改代码,以便在循环之前等待完成处理程序启动

问题

我正在尝试运行以下代码块:

let sourceCodes:[String] = self.makeCodeArray(codeString: (self.sourceDict?["Code"]!)!)

print("A")
for i in 0..<sourceCodes.count
{
    print("B")
    getRealtimeData(stopCode: sourceCodes[i], completion:
    {
        (realtimeSourceResult) in

        print("C")
        self.overallSourceResult.append(realtimeSourceResult)

        if i == sourceCodes.count - 1
        {
            print("D")
        }
        else
        {
            print("K")
        }
    })
}
但是,当前代码按以下顺序执行:A B。。。C K C K C D

如何更改代码,使其以所需的方式执行?我尝试过使用Dispatch.group,但似乎也无法使其正常工作

这是我尝试使用Dispatch.group的解决方案

let sourceCodes:[String] = self.makeCodeArray(codeString: (self.sourceDict?["Code"]!)!)
let group = DispatchGroup()

print("A")
for i in 0..<sourceCodes.count
{
    group.enter()

    print("B")
    getRealtimeData(stopCode: sourceCodes[i], completion:
    {
        (realtimeSourceResult) in

        print("C")
        self.overallSourceResult.append(realtimeSourceResult)

        if i == sourceCodes.count - 1
        {
            print("D")
        }
        else
        {
            group.leave()
            print("K")
        }
    })
}
让sourceCodes:[String]=self.makeCodeArray(codeString:[self.sourceDict?[“code”]!)
let group=DispatchGroup()
打印(“A”)

对于0中的i..您需要使用
group.wait()
。你需要移动你的
组。离开()
,所以它总是被调用<必须为每个
enter
调用code>leave
,否则
wait
将永远不会停止等待

print("A")
for i in 0..<sourceCodes.count
{
    group.enter()

    print("B")
    getRealtimeData(stopCode: sourceCodes[i], completion:
    {
        (realtimeSourceResult) in

        print("C")
        self.overallSourceResult.append(realtimeSourceResult)

        if i == sourceCodes.count - 1
        {
            print("D")
        }
        else
        {
            print("K")
        }

        group.leave() // always call this
    })

    group.wait() // don't iterate until the completion handler is done
}
打印(“A”)

对于0中的i..您需要使用
group.wait()
。你需要移动你的
组。离开()
,所以它总是被调用<必须为每个
enter
调用code>leave
,否则
wait
将永远不会停止等待

print("A")
for i in 0..<sourceCodes.count
{
    group.enter()

    print("B")
    getRealtimeData(stopCode: sourceCodes[i], completion:
    {
        (realtimeSourceResult) in

        print("C")
        self.overallSourceResult.append(realtimeSourceResult)

        if i == sourceCodes.count - 1
        {
            print("D")
        }
        else
        {
            print("K")
        }

        group.leave() // always call this
    })

    group.wait() // don't iterate until the completion handler is done
}
打印(“A”)
因为我在0。。