دوال suspend تعمل في كل مكان
كتابة منطق غير متزامن آمن متعدد المنصات باستخدام coroutines
دوال suspend تعمل في كل مكان درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Async, Written Once
Network calls and disk reads take time, so they must be async. With coroutines you write that async logic once in shared code and both apps reuse it. 🚀
What suspend Means
A suspend function can pause and resume without blocking the thread. It looks like normal sequential code but never freezes the UI.
suspend fun loadUser(): UserSequential, Not Blocking
Inside a suspend function you call other suspend functions in plain order. Each can pause, yet the line below only runs once the result is ready.
val user = loadUser()
val posts = loadPosts(user.id)It Lives in commonMain
The exact same suspend function compiles for Android and iOS. Put it in commonMain and neither platform writes its own copy.
suspend fun fetchScore(): Int = api.getScore()Calling Needs a Coroutine
You cannot call a suspend function from regular code. It must run inside a coroutine, started by a scope or another suspend function.
Suspend Calls Suspend
The simplest place to call a suspend function is another suspend function. They chain together, building bigger shared async flows from small ones.
suspend fun refresh() {
val data = loadUser()
}No Threads in Your Code
You never manually start a thread. Coroutines schedule the work for you, which is why the same code is safe on iOS and Android alike.
Return Values Feel Normal
A suspend function returns a value just like any function. There are no callbacks to nest, so shared logic stays flat and easy to read. ✨
suspend fun total(): Int =
loadCart().sumOf { it.price }Exceptions Flow Naturally
When a suspend call fails it throws an exception you can try/catch around. Error handling reads like ordinary synchronous code.
try { loadUser() } catch (e: Exception) { }A Shared Async Building Block
Treat each suspend function as a reusable unit of work. Compose them in shared code and both UIs simply await the same logic.
Why This Matters for KMP
Because suspend functions are pure Kotlin, your async business logic never drifts between platforms. Write the hard part once, share it everywhere. 🎯
Quick Check
Let us check the key idea about suspend functions.
Recap
You learned that suspend functions write async logic once: they pause without blocking, read like sequential code, and compile for every KMP target. 🎉
تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «دوال suspend تعمل في كل مكان» مجاني؟
نعم — نص درس «دوال suspend تعمل في كل مكان» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «دوال suspend تعمل في كل مكان»؟
كتابة منطق غير متزامن آمن متعدد المنصات باستخدام coroutines تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «دوال suspend تعمل في كل مكان»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- دوال suspend تعمل في كل مكان
- Dispatchers عبر المنصات
- CoroutineScope ودورة الحياة
- withContext والتزامن المنظم