<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/ThrowingDiscardingTaskGroup",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:12_Concurrency27ThrowingDiscardingTaskGroupV"
  },
  "title" : "ThrowingDiscardingTaskGroup"
}
-->

# ThrowingDiscardingTaskGroup

A throwing discarding group that contains dynamically created child tasks.

```
@frozen struct ThrowingDiscardingTaskGroup<Failure> where Failure : Error
```

## Overview

To create a discarding task group,
call the [`withDiscardingTaskGroup(returning:isolation:body:)`](/documentation/Swift/withDiscardingTaskGroup(returning:isolation: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.

Refer to [`TaskGroup`](/documentation/Swift/TaskGroup) documentation for detailed discussion of semantics shared between all task groups.

### Discarding behavior

A discarding task group eagerly discards and releases its child tasks as
soon as they complete. This allows for the efficient releasing of memory used
by those tasks, which are not retained for future `next()` calls, as would
be the case with a [`TaskGroup`](/documentation/Swift/TaskGroup).

### Cancellation behavior

A throwing discarding task group becomes canceled in one of the following ways:

- when [`cancelAll()`](/documentation/Swift/ThrowingDiscardingTaskGroup/cancelAll()) is invoked on it,
- when an error is thrown out of the `withThrowingDiscardingTaskGroup { ... }` closure,
- when the [`Task`](/documentation/Swift/Task) running this task group is canceled.

But also, and uniquely in *discarding* task groups:

- when *any* of its child tasks throws.

The group becoming canceled automatically, and cancelling all of its child tasks,
whenever *any* child task throws an error is a behavior unique to discarding task groups,
because achieving such semantics is not possible otherwise, due to the missing `next()` method
on discarding groups. Accumulating task groups can implement this by manually polling `next()`
and deciding to `cancelAll()` when they decide an error should cause the group to become canceled,
however a discarding group cannot poll child tasks for results and therefore assumes that child
task throws are an indication of a group wide failure. In order to avoid such behavior,
use a [`DiscardingTaskGroup`](/documentation/Swift/DiscardingTaskGroup) instead of a throwing one, or catch specific errors in
operations submitted using `addTask`.

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

A canceled task group can still keep adding tasks, however they will start
being immediately canceled, and may act accordingly to this. To avoid adding
new tasks to an already canceled task group, use [`addTaskUnlessCancelled(priority:operation:)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTaskUnlessCancelled(priority:operation:))
rather than the plain [`addTask(priority:operation:)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTask(priority:operation:)) which adds tasks unconditionally.

For information about the language-level concurrency model that `DiscardingTaskGroup` is part of,
see [Concurrency](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/).

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/TaskGroup``

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/ThrowingTaskGroup``

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/DiscardingTaskGroup``

## Topics

### Instance Properties

[`var isCancelled: Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/isCancelled)

A Boolean value that indicates whether the group was canceled.

[`var isEmpty: Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/isEmpty)

A Boolean value that indicates whether the group has any remaining tasks.

### Instance Methods

[`func addImmediateTask(name: String?, priority: TaskPriority?, executorPreference: consuming (any TaskExecutor)?, operation: sending () async throws -> Void)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addImmediateTask(name:priority:executorPreference:operation:))

Add a child task to the group and immediately start running it in the context of the calling thread/task.

[`func addImmediateTaskUnlessCancelled(name: String?, priority: TaskPriority?, executorPreference: consuming (any TaskExecutor)?, operation: sending () async throws -> Void) -> Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/addImmediateTaskUnlessCancelled(name:priority:executorPreference:operation:))

Add a child task to the group and immediately start running it in the context of the calling thread/task.

[`func addTask(executorPreference: (any TaskExecutor)?, priority: TaskPriority?, operation: sending () async throws -> Void)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTask(executorPreference:priority:operation:))

Adds a child task to the group.

[`func addTask(name: String?, executorPreference: (any TaskExecutor)?, priority: TaskPriority?, operation: sending () async throws -> Void)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTask(name:executorPreference:priority:operation:))

Adds a child task to the group.

[`func addTask(name: String?, priority: TaskPriority?, operation: sending () async throws -> Void)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTask(name:priority:operation:))

Adds a child task to the group.

[`func addTask(priority: TaskPriority?, operation: sending () async throws -> Void)`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTask(priority:operation:))

Adds a child task to the group.

[`func addTaskUnlessCancelled(executorPreference: (any TaskExecutor)?, priority: TaskPriority?, operation: sending () async throws -> Void) -> Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTaskUnlessCancelled(executorPreference:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`func addTaskUnlessCancelled(name: String?, executorPreference: (any TaskExecutor)?, priority: TaskPriority?, operation: sending () async throws -> Void) -> Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTaskUnlessCancelled(name:executorPreference:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`func addTaskUnlessCancelled(name: String?, priority: TaskPriority?, operation: sending () async throws -> Void) -> Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTaskUnlessCancelled(name:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`func addTaskUnlessCancelled(priority: TaskPriority?, operation: sending () async throws -> Void) -> Bool`](/documentation/Swift/ThrowingDiscardingTaskGroup/addTaskUnlessCancelled(priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`func cancelAll()`](/documentation/Swift/ThrowingDiscardingTaskGroup/cancelAll())

Cancel all of the remaining tasks in the group.

## Relationships

### Conforms To

[`Copyable`](/documentation/Swift/Copyable)

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)