TaskGroup

    A group that contains dynamically created child tasks.

    iOS
    13.0+
    macOS
    10.15+
    tvOS
    13.0+
    watchOS
    6.0+
    @frozen struct TaskGroup<ChildTaskResult> where ChildTaskResult : Sendable

    To create a task group, call the withTaskGroup(of:returning:body:) method.

    Don’t use a task group from outside the task where you created it. In most cases, the Swift type system prevents a task group from escaping like that because adding a child task to a task group is a mutating operation, and mutation operations can’t be performed from a concurrent execution context like a child task.

    Task execution order

    Tasks added to a task group execute concurrently, and may be scheduled in any order.

    Cancellation behavior

    A task group becomes cancelled in one of the following ways:

    • when cancelAll is invoked on it,

    • when the Task running this task group is cancelled.

    Since a TaskGroup is a structured concurrency primitive, cancellation is automatically propagated through all of its child-tasks (and their child tasks).

    A cancelled task group can still keep adding tasks, however they will start being immediately cancelled, and may act accordingly to this. To avoid adding new tasks to an already cancelled task group, use addTaskUnlessCancelled(priority:body:) rather than the plain addTask(priority:body:) which adds tasks unconditionally.

    For information about the language-level concurrency model that TaskGroup is part of, see Concurrency in The Swift Programming Language.

    Citizens in _Concurrency

    Members

    Citizens in _Concurrency

    where ChildTaskResult:Sendable

    Conformances

    Members

    Features