Here is the deal. I'm attempting to walk the tree. But want to do so concurrently. So each time i walk onto a node i need to concurrently walk all of it's nodes and so on.
But. I do not want to wait for the whole DispatchGroup to finish to get results since it's like a worst case scenario in Big O.
Instead i want to cancel all the other DispatchWorkItems and leave group for them in the successing one. Tried to do so with counting the task which ended. Obviously i'm doing something wrong or misunderstand how to use this.
The code below was written just for purpose of example and to test the idea.
Consider the real world situation is that in the DispatchWorkItem you can call recursively another handle
function of a current node of tree.
func handle(completion: @escaping (Int) -> Void) {
var result: Int = 0
var count = 7
let group = DispatchGroup()
let queue = DispatchQueue(label: "q", attributes: .concurrent)
var items = [DispatchWorkItem]()
let item1 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...1000 { continue }
count -= 1
group.leave()
print("left 1")
}
let item2 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...2000 { continue }
count -= 1
group.leave()
print("left 2")
}
let item3 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...6000 { continue }
count -= 1
group.leave()
print("left 3")
}
let item4 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...3000 { continue }
result = 42
items.forEach { $0.cancel() }
for _ in 0..<count {
group.leave()
}
print("ok; left 4")
}
let item5 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...50000 { continue }
count -= 1
group.leave()
print("left 5")
}
let item6 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...6000 { continue }
count -= 1
group.leave()
print("left 6")
}
let item7 = DispatchWorkItem(flags: .inheritQoS) {
for _ in 0...8000 { continue }
count -= 1
group.leave()
print("left 7")
}
items.append(item1)
items.append(item2)
items.append(item3)
items.append(item4)
items.append(item5)
items.append(item6)
items.append(item7)
for item in items {
group.enter()
queue.async(execute: item)
}
group.notify(queue: queue) {
return
}
}
test() {
handle { result in
print(result)
}
}