Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 是否有方法取消DispatchQueue concurrentPerform操作?_Swift_Macos_Grand Central Dispatch - Fatal编程技术网

Swift 是否有方法取消DispatchQueue concurrentPerform操作?

Swift 是否有方法取消DispatchQueue concurrentPerform操作?,swift,macos,grand-central-dispatch,Swift,Macos,Grand Central Dispatch,有没有办法停止concurrentPerform操作 let count = arr.count let group = DispatchGroup() group.enter() DispatchQueue.concurrentPerform(iterations: count, execute: { i in if stop { group.leave(); return } // Crashes with EXC_BAD_INSTRUCTION // .. }) grou

有没有办法停止concurrentPerform操作

let count = arr.count
let group = DispatchGroup()
group.enter()
DispatchQueue.concurrentPerform(iterations: count, execute: { i in
    if stop { group.leave(); return }  // Crashes with EXC_BAD_INSTRUCTION
    // ..
})
group.wait()

经过几次迭代后,
EXC\u BAD\u指令
会崩溃。似乎
组。leave()
未退出该操作。如何退出取消操作?基本上,我试图做的是并发循环一个数组,并在满足某些条件时将其中断,而不使用Objective-C桥来使用并发枚举。

DispatchQueue.concurrentPerform
使块执行指定的次数,并等待所有迭代完成。这是无法避免的。(如果目标队列是并发队列,则块并行运行。)

当然,如果满足某些条件,每个工作项都可以“提前返回”,但这不会影响其他工作项


您的代码崩溃,因为调度组调用
leave()
的次数多于
enter()

您可能不应该为每个数组元素创建工作项,而应该在“步长”上工作。例如,请参见为网格的每一行创建工作项的位置。(实际上所有这些都值得一读:)