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

การค้นหาแบบตอบสนองที่ส่งคืน Flow

สังเกตฐานข้อมูลใน Compose

การค้นหาแบบตอบสนองที่ส่งคืน Flow เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Static Reads Go Stale

A one-time query gives you data once, but the table keeps changing. You want the UI to refresh whenever rows are added or removed.

Return a Flow Instead

Change a DAO query to return a Flow. Room now emits a fresh list every single time the underlying table changes. 🔄

@Query("SELECT * FROM Note")
fun observeAll(): Flow<List<Note>>

No suspend on Flow Queries

A Flow query is not marked suspend. It returns the stream immediately, and emissions arrive as the data updates over time.

Room Watches the Tables

Behind the scenes Room tracks which tables a query touches. When any of them changes, it re-runs the query and emits a new value.

Collect the Flow Safely

In your ViewModel, collect the Flow inside a coroutine. Use viewModelScope so collection stops when the ViewModel clears.

viewModelScope.launch {
    dao.observeAll().collect { notes -> /* update state */ }
}

Turn Flow Into State

Convert the stream into observable UI state with stateIn, giving an initial value so the screen has something to show at once.

val notes = dao.observeAll().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

Observe It in Compose

In the Composable read the StateFlow with collectAsStateWithLifecycle. The list recomposes automatically on every change.

val notes by viewModel.notes.collectAsStateWithLifecycle()

Insert and Watch It Appear

Insert a row anywhere and the Flow emits again, so the new item shows up instantly without you refreshing manually.

Map Inside the Flow

You can transform results with map before they reach the UI, like sorting or filtering, all reactively in the stream.

dao.observeAll().map { it.sortedBy(Note::text) }

Flows Beat LiveData Here

Flow integrates cleanly with coroutines and Compose, making it the modern choice over LiveData for reactive Room queries.

One Source, Always Current

With reactive queries the database becomes your single source of truth, and the UI is just a reflection of it at every moment.

Quick Check

What happens when a Room DAO query returns a Flow?

Recap: Reactive Reads

You made queries return Flow, collected them into state, and watched Compose redraw on every change. Next you will organize this behind a repository.

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

บทเรียน “การค้นหาแบบตอบสนองที่ส่งคืน Flow” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การค้นหาแบบตอบสนองที่ส่งคืน Flow”

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

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

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

บทเรียน “การค้นหาแบบตอบสนองที่ส่งคืน Flow” ใช้เวลานานแค่ไหน

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

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

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

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

  1. เอนทิตี DAO และฐานข้อมูล
  2. การค้นหาแบบตอบสนองที่ส่งคืน Flow
  3. รูปแบบ Repository
  4. กลยุทธ์แคชแบบออฟไลน์เป็นหลัก
← กลับไปที่ Jetpack Compose Academy