Intuitively, I tried something like this:
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
for i in 0..<10 {
CATransaction.begin()
let now = DISPATCH_TIME_NOW
CATransaction.setCompletionBlock {
var delay = dispatch_time(now, 0)
dispatch_after(delay, dispatch_get_main_queue(), {
myfunc()
dispatch_semaphore_signal(semaphore)
})
}
CATransaction.commit()
}
for i in 0..<10 {
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}
//task will be executed after 10 jobs are completed.
However, it seem dispatch_semaphore_wait actually blocks dispatch_after from being executed. How to wait until all 10 async jobs are done?
Thanks!