Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Composeのライフサイクル対応を支援するLifecycleEventEffectの紹介
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
mikan
August 10, 2023
Technology
1
930
Composeのライフサイクル対応を支援するLifecycleEventEffectの紹介
YUMEMI.grow Mobile #6 での発表資料です
https://www.youtube.com/watch?v=gNd-3YjaUwg
mikan
August 10, 2023
Tweet
Share
More Decks by mikan
See All by mikan
Navigation3でViewModelにデータを渡す方法
mikanichinose
0
650
「脳に収まるコードの書き方」を読んで学んだこと
mikanichinose
1
180
RepositoryのSSoT化
mikanichinose
0
84
Kotlin Multiplatform 始めました
mikanichinose
1
140
Web APIをなぜつくるのか
mikanichinose
0
3.6k
イベントをどう管理するか
mikanichinose
3
390
ライブラリでしかお目にかかれない珍しい実装
mikanichinose
2
490
Strong Skipping Mode によってrecompositionはどう変わったのか
mikanichinose
0
370
Modeling UiEvent
mikanichinose
0
120
Other Decks in Technology
See All in Technology
「通るまでRe-run」から卒業!落ちないテストを書く勘所
asumikam
1
180
組織全体で実現する標準監視設計
yuobayashi
3
500
The_Evolution_of_Bits_AI_SRE.pdf
nulabinc
PRO
0
240
Go標準パッケージのI/O処理をながめる
matumoto
0
230
OpenClaw を Amazon Lightsail で動かす理由
uechishingo
0
200
僕、S3 シンプルって名前だけど全然シンプルじゃありません よろしくお願いします
yama3133
1
230
AWS CDK「読めるけど書けない」を脱却するファーストステップ
smt7174
3
190
OSC仙台プレ勉強会 AlmaLinuxとは
koedoyoshida
0
190
NewSQL_ ストレージ分離と分散合意を用いたスケーラブルアーキテクチャ
hacomono
PRO
4
400
内製AIチャットボットで学んだDatadog LLM Observability活用術
mkdev10
0
130
Yahoo!ショッピングのレコメンデーション・システムにおけるML実践の一例
lycorptech_jp
PRO
1
230
生成AI活用でQAエンジニアにどのような仕事が生まれるか/Support Required of QA Engineers for Generative AI
goyoki
1
270
Featured
See All Featured
HDC tutorial
michielstock
1
560
Balancing Empowerment & Direction
lara
5
950
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
200
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Rails Girls Zürich Keynote
gr2m
96
14k
Typedesign – Prime Four
hannesfritz
42
3k
Prompt Engineering for Job Search
mfonobong
0
200
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
230
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
180
Making the Leap to Tech Lead
cromwellryan
135
9.8k
So, you think you're a good person
axbom
PRO
2
2k
Transcript
Compose のライフサイクル対応 を支援する LifecycleEventEffect の紹介 YUMEMI.grow Mobile #6 一瀬喜弘(@mikanIchinose)
自己紹介 object Mikan { val name = " 一瀬喜弘" val
company = "karabiner.tech" val hobby = listOf( " 漫画", " アニメ", " ゲーム", " 折り紙", "OSS 開発・コントリビュート", ) }
目次 LifecycleEventEffect なる副作用が入ることを知る 追加されたAPI の紹介 Compose の中でLifecycle-aware な何かを扱う場合のTips まとめ
LifecycleEventEffect なる副作用が入ることを知る
alpha リリースに入った!!
これまでのライフサイクル対応 DisposbleEffect とLifecycleEventObserver を利用する val lifecycle = LocalLifecycleOwner.current.lifecycle DisposableEffect(lifecycle) {
val lifecycleObserver = LifecycleEventObserver { _, event -> if (event == Lifecycle.Event.ON_RESUME) { // do something } } lifecycle.addObserver(lifecycleObserver) onDispose { lifecycle.removeObserver(lifecycleObserver) } }
LifecycleEventEffect 1 つのライフサイクルイベントにたいして処理をフックできる @Composable fun LifecycleEventEffect( event: Lifecycle.Event, lifecycleOwner: LifecycleOwner
= LocalLifecycleOwner.current, onEvent: () -> Unit )
LifecycleEventEffect 使い方 LifecycleEventEffect(Lifecycle.Event.ON_START) { // do something onStart } //
イベントトラッキング LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { logger.trackScreenView(screen_name) } // データ更新 // 後述するLifecycle-aware なViewModel を紐付ける実装のほうがオススメ LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { viewModel.fetchData() }
LifecycleStartEffect ON_START, ON_STOP 時に処理をフックする @Composable fun LifecycleStartEffect( vararg keys: Any?,
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, effects: LifecycleStartStopEffectScope.() -> LifecycleStopOrDisposeEffectResult )
LifecycleStartEffect 使い方 onStopOrDispose :onStop とonDispose のときに処理を実行する LifecycleStartEffect { // ON_START
で実行したい処理 onStopOrDispose { // ON_STOP で実行したい処理 } } ` `
LifecycleStartEffect 使い方 @Composable fun Counter() { var count by remember
{ mutableStateOf(0) } LaunchedEffect(count) { delay(1000) count++ } LifecycleStartEffect(count) { Log.d("Counter", "onStart") onStopOrDispose { Log.d("Counter", "onStop or onDispose") } } Text(text = "$count") }
LifecycleResumeEffect ON_RESUME, ON_PAUSE 時に処理をフックする @Composable fun LifecycleResumeEffect( vararg keys: Any?,
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, effects: LifecycleResumePauseEffectScope.() -> LifecyclePauseOrDisposeEffectResult )
Lifecycle.currentStateFlow Lifecycle.currentStateAsState Lifecycle.eventFlow open val currentStateFlow: StateFlow<Lifecycle.State> @Composable fun Lifecycle.currentStateAsState():
State<Lifecycle.State> // lifecycle.currentStateFlow.collectAsState() のショートハンド val Lifecycle.eventFlow: Flow<Lifecycle.Event>
Tips: Lifecycle-aware な ViewModel を Compose に紐付 ける ライフサイクルを紐付けるだけで関数呼び出しを書かなくてよいのでタイミングを間違えることがない class
MainViewModel : ViewModel(), DefaultLifecycleObserver { override fun onResume(owner: LifecycleOwner) { // fetch data // track event // ... } } val lifecycle = LocalLifecycleOwner.current.lifecycle DisposableEffect(lifecycle) { lifecycle.addObserver(viewModel) onDispose { lifecycle.removeObserver(viewModel) } }
Tips: Lifecycle-aware な ViewModel を Compose に紐付 ける class MainViewModel
: ViewModel(), DefaultLifecycleObserver { override fun onResume(owner: LifecycleOwner) { // fetch data // track event // ... } } @Composable fun LifecycleObserver.observeLifecycleEvents(lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle) { DisposableEffect(lifecycle) { lifecycle.addObserver(this@observeLifecycleEvents) onDispose { lifecycle.removeObserver(this@observeLifecycleEvents) } } }
Tips: Lifecycle-aware な ViewModel を Compose に紐付 ける class MainViewModel
: ViewModel(), DefaultLifecycleObserver { override fun onResume(owner: LifecycleOwner) { // fetch data // track event // ... } } @Composable fun LifecycleObserver.observeLifecycleEvents(lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle) { DisposableEffect(lifecycle) { lifecycle.addObserver(this@observeLifecycleEvents) onDispose { lifecycle.removeObserver(this@observeLifecycleEvents) } } } @Composable fun GreetingScreen(viewModel: MainViewModel) { viewModel.observeLifecycleEvents() // ... }
まとめ Lifecycle.{Event, State} をcoroutine やcompose で利用するための拡張が入った Lifecycle を扱う系の副作用が3 つ入った 従来のDisposableEffect
を使ったボイラープレートを減らせそう( な気がする) LifecycleResumeEffect 、LifecycleStartEffect の実用的な使い方ワカラン