جمع Flows من Swift
ربط StateFlow بـ SwiftUI دون تسريبات
جمع Flows من Swift درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Flows Are Trickier
A Kotlin Flow emits many values over time. Swift has no built-in way to collect it, so you need a small bridge.
val users: StateFlow<List<User>>Why Not Just Await
Await fits a single result, but a Flow is a stream. You must keep listening, which means collecting inside a coroutine.
Expose a Collect Helper
A common pattern is a shared helper that takes a callback and collects the Flow, calling Swift each time a value arrives.
fun watch(onEach: (User) -> Unit)Wrap in ObservableObject
On the Swift side, an ObservableObject holds the latest value as a published property so SwiftUI redraws on each emission.
class UserStore: ObservableObject {
@Published var users = []
}Start Collecting
When the wrapper appears, call the shared watch helper and assign each emitted value to the published property.
repo.watch { self.users = $0 }Keep the Job
The collect helper returns a cancellation handle. Store that job so you can stop the stream when the view goes away.
Cancel on Disappear
In onDisappear, call cancel on the saved job so the coroutine stops and you avoid a leaked, forever-running collector.
job.cancel()StateFlow Has a Value
A StateFlow always holds a current value, so Swift can read its initial state immediately before any new emissions arrive.
Threading Matters
Make sure each value reaches Swift on the main thread, since updating SwiftUI state off the main thread causes problems.
Let SKIE Do It
The library SKIE turns Flows into Swift AsyncSequence, so you can for-await over them and skip writing manual collectors.
One Stream, Live UI
With the bridge in place, your shared Flow drives SwiftUI live: every emission updates the screen, just like on Android.
Quick Check
Let us confirm the safe way to consume a Flow from Swift.
Recap
Bridge a Flow to Swift with a collect helper plus an ObservableObject, update on the main thread, and cancel the job to avoid leaks. 🚀
الأسئلة الشائعة
هل درس «جمع Flows من Swift» مجاني؟
نعم — نص درس «جمع Flows من Swift» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «جمع Flows من Swift»؟
ربط StateFlow بـ SwiftUI دون تسريبات تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «جمع Flows من Swift»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كيفية تحويل Kotlin إلى Objective-C
- دوال Suspend باعتبارها Swift async
- جمع Flows من Swift
- @ObjCName وواجهات API الملائمة لـ Swift