@@ -695,8 +695,8 @@ To let the user stop this work,
695
695
without waiting for all of the tasks to complete,
696
696
the tasks need check for cancellation and stop running if they are canceled.
697
697
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.
700
700
Calling ` checkCancellation() ` throws an error if the task is canceled;
701
701
a throwing task can propagate the error out of the task,
702
702
stopping all of the task's work.
@@ -706,16 +706,17 @@ which lets you perform clean-up work as part of stopping the task,
706
706
like closing network connections and deleting temporary files.
707
707
708
708
[ `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
710
710
711
711
```
712
712
let photos = await withTaskGroup(of: Optional<Data>.self) { group in
713
713
let photoNames = await listPhotos(inGallery: "Summer Vacation")
714
714
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 }
717
717
return await downloadPhoto(named: name)
718
718
}
719
+ guard added else { break }
719
720
}
720
721
721
722
var results: [Data] = []
@@ -732,6 +733,11 @@ The code above makes several changes from the previous version:
732
733
[ ` TaskGroup.addTaskUnlessCancelled(priority:operation:) ` ] [ ] method,
733
734
to avoid starting new work after cancellation.
734
735
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
+
735
741
- Each task checks for cancellation
736
742
before starting to download the photo.
737
743
If it has been canceled, the task returns ` nil ` .
@@ -745,6 +751,13 @@ The code above makes several changes from the previous version:
745
751
746
752
[ `TaskGroup.addTaskUnlessCancelled(priority:operation:)` ] : https://developer.apple.com/documentation/swift/taskgroup/addtaskunlesscancelled(priority:operation:)
747
753
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
+
748
761
For work that needs immediate notification of cancellation,
749
762
use the [ ` Task.withTaskCancellationHandler(operation:onCancel:) ` ] [ ] method.
750
763
For example:
0 commit comments