Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concurrency]を読んで #541

Merged
merged 5 commits into from
Apr 13, 2024
Merged

[Concurrency]を読んで #541

merged 5 commits into from
Apr 13, 2024

Conversation

samekard-dev
Copy link
Contributor

変更の性質ごとにコミットを分けてあります。

Copy link
Owner

@stzn stzn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正ありがとうございます!
何点か確認させてください。

Comment on lines 260 to 264
let added = group.addTaskUnlessCancelled {
guard !Task.isCancelled else { return nil }
group.addTaskUnlessCancelled {
guard isCancelled == false else { return nil }
return await downloadPhoto(named: name)
}
guard added else { break }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは変更前のものが原文の通りだと思ったのですが、いかがでしょうか?
https://github.com/apple/swift-book/blob/main/TSPL.docc/LanguageGuide/Concurrency.md#task-cancellation

@@ -275,12 +262,9 @@ let photos = await withTaskGroup(of: Optional<Data>.self) { group in
上記のコードでは、以前のバージョンからいくつかの変更が加えられています:

- 各タスクは [`TaskGroup.addTaskUnlessCancelled(priority:operation:)`](https://developer.apple.com/documentation/swift/taskgroup/addtaskunlesscancelled(priority:operation:))メソッドを使用して追加され、キャンセル後に新しい作業が開始されるのを防ぎます
- `addTaskUnlessCancelled(priority:operation:)` を呼び出すたびに、コードは新しい子タスクが追加されたことを確認します。グループがキャンセルされた場合、`added` の値は `false` になります。上記の例の場合、コードは追加の写真をダウンロードしようとするのを止めます
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは原文の

After each call to addTaskUnlessCancelled(priority:operation:), the code confirms that the new child task was added. If the group is canceled, the value of added is false --- in that case, the code stops trying to download additional photos.

対応する箇所だと思いますが、どうでしょうか?

https://github.com/apple/swift-book/blob/main/TSPL.docc/LanguageGuide/Concurrency.md#task-cancellation

- 各タスクは、写真のダウンロードを開始する前に、キャンセルされたかどうかをチェックします。キャンセルされた場合、タスクは `nil` を返します
- 最後に、タスクグループは結果を集める際に `nil` 値をスキップします。`nil` を返すことでキャンセルを処理することは、タスクグループが完了した作業を破棄する代わりに、部分的な結果(キャンセル時にすでにダウンロードされていた写真)を返すことができるということです

> NOTE: タスクの外側からタスクがキャンセルされたかどうかを確認するには、型プロパティの代わりに [`Task.isCancelled` インスタンスプロパティ](https://developer.apple.com/documentation/swift/task/iscancelled-swift.property)を使用します。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらも原文の↓に対応するのかなと思います。

Note: To check whether a task has been canceled from outside that task, use the Task.isCancelled instance property instead of the type property.

https://github.com/apple/swift-book/blob/main/TSPL.docc/LanguageGuide/Concurrency.md#task-cancellation

@samekard-dev
Copy link
Contributor Author

そこが原文ですか
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/
の方を見てました。

@samekard-dev
Copy link
Contributor Author

理解しました。
docs.swift.orgの方は5.10の完成バージョンで
そこから次の段階に進んでいるのがgithub.comの方ですね。

@samekard-dev
Copy link
Contributor Author

上げていただいた3点を修正しました。

Copy link
Owner

@stzn stzn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMです。ありがとうございます😄

@stzn stzn merged commit 4a7a1b9 into stzn:master Apr 13, 2024
@samekard-dev samekard-dev deleted the working branch April 14, 2024 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants