مراقبة الجداول باعتبارها Flow
الحصول على تحديثات تفاعلية عند تغيّر قاعدة البيانات
مراقبة الجداول باعتبارها Flow درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «مراقبة الجداول باعتبارها Flow»؟
الحصول على تحديثات تفاعلية عند تغيّر قاعدة البيانات تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «مراقبة الجداول باعتبارها Flow»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إضافة SQLDelight وكتابة مخططات .sq
- إعداد SqlDriver للمنصة
- إدراج الصفوف والاستعلام عنها وتحديثها
- مراقبة الجداول باعتبارها Flow