I’m checking myself to see if I understand correctly. The following is an excerpt from Chapter 4 Groups & Semaphores. I set two similar phrases to bold and italics that I want to compare. The first says that the wait method will “block the current queue”, and the second phrase says that it “blocks the current thread”. I’m being very particular in distinguishing between “queue” and “thread”. Do I understand correctly that the wait method actually blocks the current thread on the current queue? I believe I got that right.
Synchronous waiting
There be dragons here!
If, for some reason, you can’t respond asynchronously to the group’s completion notification, then you can instead use the wait
method on the dispatch group. This is a synchronous method that will block the current queue until all the jobs have finished. It takes an optional parameter which specifies how long to wait for the tasks to complete. If not specified then there is an infinite wait time:
let group = DispatchGroup()
someQueue.async(group: group) { ... }
someQueue.async(group: group) { ... }
someOtherQueue.async(group: group) { ... }
if group.wait(timeout: .now() + 60) == .timedOut {
print("The jobs didn’t finish in 60 seconds")
}
Note: Remember, this blocks the current thread; never ever call
wait
on the main queue.