Skip to content

Commit 2bcbf22

Browse files
authored
Fixes task cancellation sample code (#279)
The code listing tried to access the `isCancelled` property without specifying `Task.isCancelled`. That would work in the scope of a task, because there's an instance property with that name. But it doesn't work in the context of calling `addTaskUnlessCancelled(priority:operation:)` to adding a child task to a task group -- in that case, you need to use the type property.
2 parents 72f6eb7 + be6577a commit 2bcbf22

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

TSPL.docc/LanguageGuide/Concurrency.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ To let the user stop this work,
695695
without waiting for all of the tasks to complete,
696696
the tasks need check for cancellation and stop running if they are canceled.
697697
There are two ways a task can do this:
698-
by calling the [`Task.checkCancellation()`][] method,
699-
or by reading the [`Task.isCancelled`][] property.
698+
by calling the [`Task.checkCancellation()`][] type method,
699+
or by reading the [`Task.isCancelled`][`Task.isCancelled` type] type property.
700700
Calling `checkCancellation()` throws an error if the task is canceled;
701701
a throwing task can propagate the error out of the task,
702702
stopping all of the task's work.
@@ -706,16 +706,17 @@ which lets you perform clean-up work as part of stopping the task,
706706
like closing network connections and deleting temporary files.
707707

708708
[`Task.checkCancellation()`]: https://developer.apple.com/documentation/swift/task/3814826-checkcancellation
709-
[`Task.isCancelled`]: https://developer.apple.com/documentation/swift/task/3814832-iscancelled
709+
[`Task.isCancelled` type]: https://developer.apple.com/documentation/swift/task/iscancelled-swift.type.property
710710

711711
```
712712
let photos = await withTaskGroup(of: Optional<Data>.self) { group in
713713
let photoNames = await listPhotos(inGallery: "Summer Vacation")
714714
for name in photoNames {
715-
group.addTaskUnlessCancelled {
716-
guard isCancelled == false else { return nil }
715+
let added = group.addTaskUnlessCancelled {
716+
guard !Task.isCancelled else { return nil }
717717
return await downloadPhoto(named: name)
718718
}
719+
guard added else { break }
719720
}
720721
721722
var results: [Data] = []
@@ -732,6 +733,11 @@ The code above makes several changes from the previous version:
732733
[`TaskGroup.addTaskUnlessCancelled(priority:operation:)`][] method,
733734
to avoid starting new work after cancellation.
734735

736+
- After each call to `addTaskUnlessCancelled(priority:operation:)`,
737+
the code confirms that the new child task was added.
738+
If the group is canceled, the value of `added` is `false` ---
739+
in that case, the code stops trying to download additional photos.
740+
735741
- Each task checks for cancellation
736742
before starting to download the photo.
737743
If it has been canceled, the task returns `nil`.
@@ -745,6 +751,13 @@ The code above makes several changes from the previous version:
745751

746752
[`TaskGroup.addTaskUnlessCancelled(priority:operation:)`]: https://developer.apple.com/documentation/swift/taskgroup/addtaskunlesscancelled(priority:operation:)
747753

754+
> Note:
755+
> To check whether a task has been canceled from outside that task,
756+
> use the [`Task.isCancelled`][`Task.isCancelled` instance] instance property
757+
> instead of the type property.
758+
759+
[`Task.isCancelled` instance]: https://developer.apple.com/documentation/swift/task/iscancelled-swift.property
760+
748761
For work that needs immediate notification of cancellation,
749762
use the [`Task.withTaskCancellationHandler(operation:onCancel:)`][] method.
750763
For example:

0 commit comments

Comments
 (0)