Upgrade to Pro — share decks privately, control downloads, hide ads and more …

"おまじない"を卒業する ボイラープレート再入門

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for shun shun
April 22, 2026

 "おまじない"を卒業する ボイラープレート再入門

Avatar for shun

shun

April 22, 2026

Other Decks in Technology

Transcript

  1. 共通 • StateFlow/Flow から流れてくる値を collect し て State に書き込む •

    Composable が Composition から外れると collect を停止する collectAsState • Android のライフサイクルを考慮しない collectAsStateWithLifecycle • Android のライフサイクルを考慮する • アプリがバックグラウンド状態の時にcollect を停止する collectAsStateと‥WithLifecycleの違い 引用:https://medium.com/androiddevelopers/consuming- flows-safely-in-jetpack-compose-cde014d0d5a3
  2. これらを踏まえて collectAsState と collectAsStateWithLifecycle それぞれどのようなユースケースで使 用するべきか collectAsState • マルチプラットフォームでの開発(?) collectAsStateWithLifecycle

    • それ以外全て 原則 collectAsStateWithLifecycle を使うべきと考えました。 バックグラウンド時も collect を続けることは、不要な Recomposition の発生や、通信・位置情報の更 新を維持することになるため、CPU・メモリ・データ通信量を無駄に消費します。 collectAsStateWithLifecycle なら stateIn(WhileSubscribed(5000)) と組み合わせることで collectが停止し た5秒後に上流の流れまで止めることが可能です。 まとめ