Task
A unit of asynchronous work.
Declaration
@frozen struct Task<Success, Failure> where Success : Sendable, Failure : ErrorOverview
When you create an instance of Task, you provide a closure that contains the work for that task to perform. Tasks can start running immediately after creation; you don’t explicitly start or schedule them. After creating a task, you use the instance to interact with it — for example, to wait for it to complete or to cancel it. It’s not a programming error to discard a reference to a task without waiting for that task to finish or canceling it. A task runs regardless of whether you keep a reference to it. However, if you discard the reference to a task, you give up the ability to wait for that task’s result or cancel the task.
To support operations on the current task, which can be either a detached task or child task, Task also exposes class methods like yield(). Because these methods are asynchronous, they’re always invoked as part of an existing task.
Only code that’s running as part of the task can interact with that task. To interact with the current task, you call one of the static methods on Task.
A task’s execution can be seen as a series of periods where the task ran. Each such period ends at a suspension point or the completion of the task. These periods of execution are represented by instances of PartialAsyncTask. Unless you’re implementing a custom executor, you don’t directly interact with partial tasks.
For information about the language-level concurrency model that Task is part of, see Concurrency in The Swift Programming Language.
Task Cancellation
Tasks include a shared mechanism for indicating cancellation, but not a shared implementation for how to handle cancellation. Depending on the work you’re doing in the task, the correct way to stop that work varies. Likewise, it’s the responsibility of the code running as part of the task to check for cancellation whenever stopping is appropriate. In a long-task that includes multiple pieces, you might need to check for cancellation at several points, and handle cancellation differently at each point. If you only need to throw an error to stop the work, call the Task.checkCancellation() function to check for cancellation. Other responses to cancellation include returning the work completed so far, returning an empty result, or returning nil.
Cancellation is a purely Boolean state; there’s no way to include additional information like the reason for cancellation. This reflects the fact that a task can be canceled for many reasons, and additional reasons can accrue during the cancellation process.
Task closure lifetime
Tasks are initialized by passing a closure containing the code that will be executed by a given task.
After this code has run to completion, the task has completed, resulting in either a failure or result value, this closure is eagerly released.
Retaining a task object doesn’t indefinitely retain the closure, because any references that a task holds are released after the task completes. Consequently, tasks rarely need to capture weak references to values.
For example, in the following snippet of code it is not necessary to capture the actor as weak, because as the task completes it’ll let go of the actor reference, breaking the reference cycle between the Task and the actor holding it.
struct Work: Sendable {}
actor Worker {
var work: Task<Void, Never>?
var result: Work?
deinit {
// even though the task is still retained,
// once it completes it no longer causes a reference cycle with the actor
print("deinit actor")
}
func start() {
work = Task {
print("start task work")
try? await Task.sleep(for: .seconds(3))
self.result = Work() // we captured self
print("completed task work")
// but as the task completes, this reference is released
}
// we keep a strong reference to the task
}
}And using it like this:
await Worker().start()Note that the actor is only retained by the start() method’s use of self, and that the start method immediately returns, without waiting for the unstructured Task to finish. Once the task is completed and its closure is destroyed, the strong reference to the actor is also released allowing the actor to deinitialize as expected.
Therefore, the above call will consistently result in the following output:
start task work
completed task work
deinit actorTopics
Creating a Task
init(name:priority:operation:)init(name:priority:operation:)init(name:executorPreference:priority:operation:)init(name:executorPreference:priority:operation:)currentPrioritybasePrioritywithTaskPriorityEscalationHandler(operation:onPriorityEscalated:)
Creating a Detached Task
detached(name:priority:operation:)detached(name:priority:operation:)detached(name:executorPreference:priority:operation:)detached(name:executorPreference:priority:operation:)
Creating a Task that Starts Immediately
immediate(name:priority:executorPreference:operation:)immediate(name:priority:executorPreference:operation:)immediateDetached(name:priority:executorPreference:operation:)immediateDetached(name:priority:executorPreference:operation:)
Accessing Results
Accessing the Current Task’s Name
Canceling Tasks
CancellationErrorcancel()isCancelledisCancelledcheckCancellation()withTaskCancellationHandler(handler:operation:)withTaskCancellationHandler(operation:onCancel:isolation:)
Suspending Execution
Escalating Tasks
Comparing Tasks
Deprecated
Task.GroupTask.HandleTask.PriorityCancellationError()getResult()get()get()sleep(_:)suspend()runDetached(priority:operation:)runDetached(priority:operation:)withCancellationHandler(handler:operation:)withGroup(resultType:returning:body:)
Default Implementations
See Also
Tasks
TaskGroupwithTaskGroup(of:returning:isolation:body:)ThrowingTaskGroupwithThrowingTaskGroup(of:returning:isolation:body:)TaskPriorityDiscardingTaskGroupwithDiscardingTaskGroup(returning:isolation:body:)ThrowingDiscardingTaskGroupwithThrowingDiscardingTaskGroup(returning:isolation:body:)UnsafeCurrentTask