I was trying following code in playground, but it seems that they are not working as I expected.
Two group_async operations cause about 5~6 seconds in total on my mac.
- When I set the timeout time to DispatchTime.now() + 10, "test returns" and "done" are both printed.
- When I set the timeout time to DispatchTime.now() + 1 (some value make the group timed out), nothing is printed except the printing codes in two group_async operations.
What I want is to suspend the group and do some clean-up when timed out, and do some other further operations when group successfully finished. Any advice is appreciated. Thanks.
import Dispatch
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue.global(qos: .utility)
func test() {
let group = DispatchGroup()
__dispatch_group_async(group, queue) {
var a = [String]()
for i in 1...999 {
a.append(String(i))
print("appending array a...")
}
print("a finished")
}
__dispatch_group_async(group, queue) {
var b = [String]()
for i in 1...999 {
b.append(String(i))
print("appending array b...")
}
print("b finished")
}
let result = group.wait(timeout: DispatchTime.now() + 10)
if result == .timedOut {
group.suspend()
print("timed out")
}
print("test returns")
}
queue.async {
test()
print("done")
}