DevPrep

All topics › Swift Concurrency (async/await, actors)

What is structured concurrency?

Middle PRO

The essence

Structured concurrency is the principle that child tasks live no longer than the scope that created them. A parent function cannot return until all of its child tasks have finished, thrown or been cancelled. Tasks form a tree, and cancellation, errors and priority propagate automatically along it. In Swift the structured constructs are async let and withTaskGroup / withThrowingTaskGroup / withDiscardingTaskGroup. The unstructured ones are Task { } and Task.detached { }: their lifetime is tied to nothing.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store