давай попробуем!В следующем примере выполняем простую работу двумя различными способами: сначала асинхронно отправляем все задания в очередь .default .concurrent, затем используем DispatchQueue.concurrentPerform.
DispatchQueue.concurrentPerform - очень приятная и простая в использовании конструкция.
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let q = DispatchQueue(label: "internal", qos: .utility, attributes: .concurrent)
func job()->String {
var sum = 0
for i in 1...1000 {
let r = Int.random(in: 0..<i)
sum += r
}
let res = sum.description
return res
}
func asyncFoo(on: DispatchQueue, id: Int, completition: @escaping (_ id: Int, _ result: String)->()) {
on.async {
let res = job()
completition(id, res)
}
}
let group = DispatchGroup()
var start = Date()
for i in 0..<10 {
group.enter() // enter the group before the task starts
asyncFoo(on: q, id: i) { (id, result) in
print("id:", id, i, result)
group.leave() // leave the group when task finished
}
}
group.notify(queue: .main) {
var stop = Date()
print("all asynchronously dispatched done in", stop.timeIntervalSince(start), "seconds")
let task: (Int)->() = { i in
let res = job()
print("id:", i, res)
}
print("continue again ...")
start = Date()
DispatchQueue.concurrentPerform(iterations: 10, execute: task)
stop = Date()
print("all .concurrentPerform done in", stop.timeIntervalSince(start), "seconds")
PlaygroundPage.current.finishExecution()
}
print("continue execution ...")
а как насчет результата?
continue execution ...
id: 7 7 251189
id: 2 2 252628
id: 8 8 248525
id: 5 5 248212
id: 0 0 254412
id: 3 3 255094
id: 6 6 260566
id: 1 1 242460
id: 9 9 247018
id: 4 4 246296
all asynchronously dispatched done in 0.10741996765136719 seconds
continue again ...
id: 2 248549
id: 3 245366
id: 7 242868
id: 8 252247
id: 0 250905
id: 4 249909
id: 6 247525
id: 9 246204
id: 1 253908
id: 5 249081
all .concurrentPerform done in 0.05399894714355469 seconds
Если доступно, я предпочитаю использовать .concurrentPerform, но это действительно зависит ... Не существует API для изменения чего-либо в .concurrentPerform, но большинствоскорее всего, это будет ваш лучший исполнитель.