Skip to content

Commit be6577a

Browse files
committed
Fix minor code listing issues
Ignoring the value returned by addTaskUnlessCancelled(...) produces a warning. This version uses the value to stop early after cancellation. Explicit comparison with true/false doesn't appear anywhere else in TSPL (except for some hidden assertions in old swifttest blocks). Use the Boolean value on its own to match style.
1 parent be75b85 commit be6577a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

TSPL.docc/LanguageGuide/Concurrency.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,11 @@ like closing network connections and deleting temporary files.
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 Task.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`.

0 commit comments

Comments
 (0)