0Pricing
Kotlin Multiplatform Academy · درس

كشف النتائج على هيئة Flow

بث بيانات Repository إلى الواجهتين بشكل تفاعلي

كشف النتائج على هيئة Flow درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Beyond One-Shot Calls

A suspend function returns once, but real screens want updates over time. That is exactly what Flow gives you.

Return a Flow

Change the repository to return a Flow of your domain type, so callers can react to a stream of results.

fun observeUsers(): Flow<List<User>>

Emit With flow Builder

Build a stream using the flow builder and call emit to push each new value to whoever is collecting.

fun observeUsers() = flow { emit(getUsers()) }

Suspend Inside Flow

Inside the builder you can freely call suspend functions, so your network fetch fits right in.

Combine With Local Data

A Flow can emit cached data first, then a fresh network result, giving the UI an instant view that updates.

flow {
    emit(loadCached())
    emit(fetchFresh())
}

Map the Stream

Use the map operator on the Flow to transform each emission before the UI ever sees it.

observeUsers().map { it.sortedBy(User::name) }

Catch Errors in the Flow

Wrap the stream with catch so a network failure becomes a graceful emission instead of a crash.

observeUsers().catch { emit(emptyList()) }

Collect on Each Platform

Both apps collect the Flow: Compose with collectAsState, and SwiftUI through a small bridge.

scope.launch { repo.observeUsers().collect { render(it) } }

Still Shared, Still Clean

The Flow logic lives in commonMain, so both platforms share one reactive data source with no copies.

A Single Source of Truth

Exposing data as a Flow gives the whole app one source of truth that everyone observes and trusts.

The Reactive Repository

Here is the repository as a stream: it emits fresh data and never lets a failure break the collector.

fun observeUsers(): Flow<List<User>> = flow {
    emit(api.getUsers().toDomain())
}.catch { emit(emptyList()) }

Quick Check

Why expose repository data as a Flow instead of a single suspend return?

Recap

Returning a Flow turns the repository into a reactive source: it emits updates over time, both apps collect it, and errors stay contained.

الأسئلة الشائعة

هل درس «كشف النتائج على هيئة Flow» مجاني؟

نعم — نص درس «كشف النتائج على هيئة Flow» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «كشف النتائج على هيئة Flow»؟

بث بيانات Repository إلى الواجهتين بشكل تفاعلي تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «كشف النتائج على هيئة Flow»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف واجهة Repository
  2. وضع API خلف Repository
  3. تحويل DTOs إلى نماذج النطاق
  4. كشف النتائج على هيئة Flow
← العودة إلى Kotlin Multiplatform Academy