Overview
概要
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
.
タスクは、ある共有された仕組みを取り消しを指し示すために含みます、しかしどのように取り消しを扱うかのための共有された実装ではありません。あなたがそのタスクにおいて行っている仕事に依存して、その仕事を止める正しい方法は様々です。同じように、停止することが適切である時はいつでも、取り消しを確認するのは、タスクの一部として動作しているコードの責任です。複数の断片を含む長いタスクにおいて、あなたは取り消しをいくつかの地点で調べる、そして取り消しを各地点で異なって取り扱う必要があるかもしれません。あなたはあるエラーをスローしてその仕事を止める必要があるだけならば、Task.checkCancellation()
関数を呼び出して取り消しを調べてください。取り消しへの他の応答は、これまでに完了した仕事を返す、空の結果を返す、または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.
取り消しはある純粋なブール状態です;取り消しの理由のような追加情報を含める方法はありません。これは、あるタスクは多くの理由のために取り消されることがありうるという事実を反映します、そして追加的な理由は取り消しプロセスの間に収集できます。