Dispatchers عبر المنصات
اختيار Dispatchers الموجودة في Android وiOS
Dispatchers عبر المنصات درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Where Work Actually Runs
A coroutine still runs on some thread. A dispatcher decides which thread or pool it uses, and that choice matters on each platform.
Meet Dispatchers.Default
Use Dispatchers.Default for CPU-heavy work like parsing or sorting. It exists on Android and iOS, so shared code can rely on it.
withContext(Dispatchers.Default) { sort(data) }Dispatchers.Main for UI
Dispatchers.Main runs on the platform UI thread. It is available in common code, but only update screens from here.
withContext(Dispatchers.Main) { render(state) }The IO Dispatcher Gap
Watch out: Dispatchers.IO exists on the JVM and Android but not in plain common code on every target. Do not assume it is everywhere.
Stay in Common-Safe Land
For portable shared logic, prefer Default and Main. They are guaranteed across the targets KMP commonly supports.
Switching with withContext
You move work between threads using withContext. Compute on Default, then hop to Main to publish the result, all in one function.
val r = withContext(Dispatchers.Default) { heavy() }Dispatchers Are Pluggable
A dispatcher is just a CoroutineContext element. You can pass one in, which makes shared code easy to configure and test.
class Loader(val io: CoroutineDispatcher)Inject for Testability
Passing the dispatcher in lets tests swap a deterministic one. Hard-coding it makes shared async logic harder to test. 🧪
iOS Runs Them Too
On iOS these dispatchers map onto the right native queues automatically. Your shared code does not care about GCD details.
One Choice, Both Platforms
Because the dispatcher names are common, the same line schedules work correctly on Android and iOS. No platform branches in your logic. ✨
Pick the Right Tool
Rule of thumb: Default for computation, Main for UI updates, and inject anything IO-like so it stays portable. 🎯
Quick Check
Pick the dispatcher choice that is safest for shared KMP code.
Recap
You learned to pick dispatchers wisely: Default for computation, Main for UI, and inject IO-style ones since they are not on every target. 🎉
الأسئلة الشائعة
هل درس «Dispatchers عبر المنصات» مجاني؟
نعم — نص درس «Dispatchers عبر المنصات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «Dispatchers عبر المنصات»؟
اختيار Dispatchers الموجودة في Android وiOS تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «Dispatchers عبر المنصات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- دوال suspend تعمل في كل مكان
- Dispatchers عبر المنصات
- CoroutineScope ودورة الحياة
- withContext والتزامن المنظم