Type Method 型メソッド

detached(priority:operation:)

Runs the given nonthrowing operation asynchronously as part of a new top-level task.

Declaration 宣言

@discardableResult static func detached(priority: TaskPriority? = nil, operation: @escaping @Sendable () async -> Success) -> Task<Success, Failure>
Available when Failure is Never. FailureNeverである時に利用可能です。

Parameters パラメータ

priority

The priority of the task.

operation

The operation to perform.

Return Value 戻り値

A reference to the task.

Discussion 解説

Don’t use a detached task if it’s possible to model the operation using structured concurrency features like child tasks. Child tasks inherit the parent task’s priority and task-local storage, and canceling a parent task automatically cancels all of its child tasks. You need to handle these considerations manually with a detached task.

You need to keep a reference to the detached task if you want to cancel it by calling the Task.cancel() method. Discarding your reference to a detached task doesn’t implicitly cancel that task, it only makes it impossible for you to explicitly cancel the task.

See Also 参照

Creating a Task タスクを作成する