0Pricing
Kotlin Multiplatform Academy · บทเรียน

สังเกตตารางเป็น Flow

รับการอัปเดตแบบตอบสนองเมื่อฐานข้อมูลเปลี่ยนแปลง

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

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

Why Observe Instead of Poll

Re-running a query by hand misses changes. SQLDelight can push fresh results to you whenever the table changes.

Add the Coroutines Extension

Reactive queries need the coroutines-extensions artifact, which adds Flow support on top of your generated queries.

implementation("app.cash.sqldelight:coroutines-extensions:2.0.2")

Turn a Query into a Flow

Call asFlow on any select query to get a Flow that re-emits whenever the underlying table is written.

val flow = queries.selectAll().asFlow()

Map to Results

asFlow gives a query holder, so chain mapToList to emit a ready Kotlin list each time the data changes.

val players = queries.selectAll()
    .asFlow()
    .mapToList(Dispatchers.IO)

Single-Row Streams

For one row use mapToOneOrNull, which emits the row or null and updates automatically as that row changes.

queries.selectById(1)
    .asFlow()
    .mapToOneOrNull(Dispatchers.IO)

Collect in Shared Code

You consume the stream by collecting it. Every insert, update or delete to that table triggers a fresh emission.

players.collect { list -> render(list) }

Expose It as StateFlow

Wrap the Flow in a StateFlow so your shared ViewModel holds the latest list as a single source of truth.

val state = players.stateIn(scope, SharingStarted.Eagerly, emptyList())

Android Collects with Compose

On Android, collectAsState turns the StateFlow into Compose state, so the list recomposes automatically on every change.

val list by viewModel.state.collectAsState()

iOS Collects from Swift

On iOS you observe the same StateFlow from Swift and update SwiftUI, so both apps stay in sync with the database.

One Write, Both UIs Update

Insert a row anywhere and every active Flow re-emits. The database becomes the live source driving both platforms.

Pick the Right Dispatcher

Run mapping off the main thread by passing a background dispatcher, keeping the UI smooth while queries run.

Quick Check

You collect selectAll().asFlow().mapToList(...). When does it emit again?

Recap: Live Data

You turned queries into Flows, mapped them to results, exposed StateFlow, and let both UIs update on every write. Your shared database is now reactive. ✅

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

บทเรียน “สังเกตตารางเป็น Flow” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “สังเกตตารางเป็น Flow”

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

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

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

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

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

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

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

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

  1. เพิ่ม SQLDelight และเขียนสคีมา .sq
  2. การตั้งค่า SqlDriver ตามแพลตฟอร์ม
  3. แทรก คิวรี และอัปเดตแถว
  4. สังเกตตารางเป็น Flow
← กลับไปที่ Kotlin Multiplatform Academy