今週は第58回を開催しました!
引き続きKotlin Hands-onをすすめています。
前回の様子はコチラ↓
[第58回の様子]
2021/3/31に第58回を開催した。
参加メンバーは自分をいれて4名。
今回は前回のつづきの内容だったので同じ人がドライバを担当し、自分はナビゲータ役で参加した。
勉強会本編の内容としては、ひき続きIntroduction to Coroutines and Channelsハンズオンを進めた。第6章 Structured Concurrencyがようやくおわった。coroutineハンズオンもあと少し!
[学んだことや疑問点]
- Introduction to Coroutines and Channels: 6. Structured Concurrency
interface Contributors {
fun loadContributors() {
// ...
when (getSelectedVariant()) {
CONCURRENT -> {
launch {
val users = loadContributorsConcurrent(req)
updateResults(users, startTime)
}.setUpCancellation() // #1
}
}
}
private fun Job.setUpCancellation() {
val loadingJob = this // #2
// cancel the loading job if the 'cancel' button was clicked:
val listener = ActionListener {
loadingJob.cancel() // #3
updateLoadingStatus(CANCELED)
}
addCancelListener(listener)
// update the status and remove the listener after the loading job is completed
}
}
- 参加者の間で話題になったのは、loadingJobへの参照をActionListenerが保持していて大丈夫なのか?という点
- 「// update the status and remove the listener after the loading job is completed」のコメントの部分できっとうまくやっているんだろうな...(未確認
- coroutineScope、async、launchbなどで作成された新しいスコープは、必ず外側のスコープからコンテキストを継承する
- ディスパッチャはコンテキストの一部である
- コンテキストの要素は、必要に応じて子供のcoroutineを起動する際に変更できる
- AndroidなどのUIアプリケーションでは、CoroutineDispatchers.Mainをトップレベルのcoroutineで指定するのがよくあるパターン:別スレッドで処理を実行したい場合には異なるディスパッチャを指定してcoroutineを起動する
[まとめ]
モブプログラミング・スタイルで、Introduction to Coroutines and Channelsハンズオンを進めた。
Coroutine、CoroutineContext、CoroutineScope、Dispatcherなどについて少し理解が深まった気がする。
今週のプルリクエストは無し。