0Pricing
Jetpack Compose Academy · Ders

stateIn ve WhileSubscribed

Flow'ları önbelleğe alın ve kimse dinlemediğinde işi durdurun.

stateIn ve WhileSubscribed, CoddyKit'te ücretsiz bir Jetpack Compose Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Jetpack Compose Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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.Eagerly

Meet 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. 🎯

Sıkça Sorulan Sorular

“stateIn ve WhileSubscribed” dersi ücretsiz mi?

Evet — “stateIn ve WhileSubscribed” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Jetpack Compose Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.

“stateIn ve WhileSubscribed” dersinde ne öğreneceğim?

Flow'ları önbelleğe alın ve kimse dinlemediğinde işi durdurun. Jetpack Compose Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Jetpack Compose Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Jetpack Compose Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“stateIn ve WhileSubscribed” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Jetpack Compose Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Jetpack Compose Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. StateFlow ve SharedFlow
  2. Flow'ları Eşleme ve Birleştirme
  3. stateIn ve WhileSubscribed
  4. Kanallarla Tek Seferlik Olaylar
← Jetpack Compose Academy Sayfasına Dön