Difference between synchronous and asynchronous tasks
The essence
The terms are about whether the caller waits for the task to finish.
- Synchronous (
queue.sync { }) — the calling thread blocks until the task has run, and only then continues. As an optimization GCD usually runs the block right on the calling thread (no new thread is created), but that is not a contract:DispatchQueue.main.syncfrom a background thread runs the block on the main thread. - Asynchronous (
queue.async { }) — the task is enqueued and the caller continues immediately. When and on which thread the block runs is up to the queue: serial — after all previously enqueued tasks, concurrent — possibly in parallel with them.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep