What's the difference between Task { } and Task.detached { }?
The essence
Both create an unstructured task: it is not a child, the parent does not wait for it, and the parent's cancellation does not reach it. The difference is what gets inherited from the creation context.
Task { }inherits priority, task-local values and the actor isolation of the context: created inside a@MainActormethod, it runs on main. In Swift 6 an instance actor's isolation is inherited when the closure captures that actor'sself(the@_inheritActorContextattribute).Task.detached { }inherits nothing: no priority (it is.unspecifiedunless given), no task-locals, no isolation — the body isnonisolatedand runs on the cooperative pool.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep