0Pricing
Jetpack Compose Academy · บทเรียน

stateIn และ WhileSubscribed

แคช Flow และหยุดงานเมื่อไม่มีผู้รับฟัง

stateIn และ WhileSubscribed เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.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. 🎯

คำถามที่พบบ่อย

บทเรียน “stateIn และ WhileSubscribed” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “stateIn และ WhileSubscribed” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “stateIn และ WhileSubscribed”

แคช Flow และหยุดงานเมื่อไม่มีผู้รับฟัง คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “stateIn และ WhileSubscribed” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. StateFlow กับ SharedFlow
  2. แมปและรวม Flow
  3. stateIn และ WhileSubscribed
  4. เหตุการณ์ครั้งเดียวด้วย Channel
← กลับไปที่ Jetpack Compose Academy