【发布时间】:2017-03-06 07:04:28
【问题描述】:
我有以下代码:-
extension Collection {
// EZSE : A parralelized map for collections, operation is non blocking
public func pmap<R>(_ each: (Self.Iterator.Element) -> R) -> [R?] {
let indices = indicesArray()
var res = [R?](repeating: nil, count: indices.count)
DispatchQueue.concurrentPerform(iterations: indices.count) { (index) in
let elementIndex = indices[index]
res[index] = each(self[elementIndex])
}
// Above code is non blocking so partial exec on most runs
return res
}
/// EZSE : Helper method to get an array of collection indices
private func indicesArray() -> [Self.Index] {
var indicesArray: [Self.Index] = []
var nextIndex = startIndex
while nextIndex != endIndex {
indicesArray.append(nextIndex)
nextIndex = index(after: nextIndex)
}
return indicesArray
}
}
在此处的return语句res中,它经常在部分执行完成的情况下返回。有道理,并发 Perform 是非阻塞的。我不确定如何继续等待它。我应该使用调度组/期望之类的东西还是有一些更简单更优雅的方法?本质上,我正在快速寻找一个简单的等待通知抽象。
【问题讨论】:
标签: ios swift concurrency parallel-processing