stateInとWhileSubscribed
Flowをキャッシュし、誰も監視していないときは処理を停止します。
「stateInとWhileSubscribed」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Cold Flow to Hot State
UI state should be hot and cached, but your transformed flows are cold. The stateIn operator converts a cold flow into a StateFlow.
What stateIn Does
stateIn launches the upstream flow in a scope, shares one collection across all collectors, and caches the latest value for new ones.
val ui = source.stateIn(
viewModelScope, started, initial)It Needs a Scope
The first argument is a scope, usually viewModelScope, which ties the running flow to the ViewModel lifecycle.
flow.stateIn(viewModelScope, ...)It Needs an Initial Value
Because a StateFlow always holds a value, stateIn requires an initial one to show before the first real emission arrives.
flow.stateIn(scope, started,
initialValue = UiState.Loading)The Sharing Strategy
The middle argument is the SharingStarted policy. It decides exactly when the upstream flow starts and stops running.
Eagerly and Lazily
SharingStarted.Eagerly starts the flow immediately and never stops it, while Lazily starts on the first collector and keeps going.
started = SharingStarted.EagerlyMeet WhileSubscribed
WhileSubscribed only runs the upstream flow while there is at least one active collector, then stops it when the last one leaves.
started = SharingStarted.WhileSubscribed()The Five Second Timeout
Passing a small timeout to WhileSubscribed keeps the flow alive briefly after the last collector, surviving quick rotations.
SharingStarted.WhileSubscribed(5000)Why the Timeout Helps
During a screen rotation the old collector leaves and a new one arrives moments later. The timeout avoids a needless restart in between.
Stops Wasted Work
With WhileSubscribed a database or network flow pauses when the screen is off, then resumes when the user returns. Battery wins. 🔋
A Full stateIn Recipe
Put it together: transform your data, then call stateIn with the scope, WhileSubscribed, and an initial loading state.
val ui = repo.flow().map { UiState(it) }
.stateIn(viewModelScope,
SharingStarted.WhileSubscribed(5000),
UiState.Loading)Quick Check
Reason about subscriber-driven lifecycles.
Recap: stateIn & WhileSubscribed
Well done! stateIn turns a cold flow into a cached StateFlow, and WhileSubscribed runs it only while observed so you stop wasting work. 🎯
よくある質問
「stateInとWhileSubscribed」レッスンは無料ですか?
はい。「stateInとWhileSubscribed」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「stateInとWhileSubscribed」で何を学びますか?
Flowをキャッシュし、誰も監視していないときは処理を停止します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「stateInとWhileSubscribed」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- StateFlowとSharedFlow
- Flowをマッピングして結合する
- stateInとWhileSubscribed
- Channelによる一度きりのイベント