0Pricing
Kotlin Multiplatform Academy · درس

CoroutineScope ودورة الحياة

ربط العمل غير المتزامن المشترك بالنطاق المناسب وإلغاؤه

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

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

Coroutines Need a Home

Every coroutine belongs to a CoroutineScope. The scope decides how long the work may live and when it should stop.

Why Scope Equals Lifecycle

Tie a scope to a screen or session. When that thing ends, the scope cancels its coroutines so no work runs against a dead UI.

Launch Starts the Work

You begin a coroutine with launch on a scope. It runs the block and tracks it as part of that scope.

scope.launch { refresh() }

Build a Scope in Shared Code

Create one with a Job and a dispatcher. This shared scope can drive async work for a ViewModel both apps use.

val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

SupervisorJob Isolates Failures

A SupervisorJob stops one failed child from cancelling its siblings. One broken request will not kill the whole screen.

Cancel to Clean Up

Call cancel when the owner goes away. Pending network or timer coroutines stop immediately, preventing leaks and stray updates.

fun onCleared() { scope.cancel() }

Cancellation Is Cooperative

Coroutines check for cancellation at suspension points. Your suspend functions stop cleanly, which is why you rarely need manual flags.

Android Gives You One

On Android a ViewModel already offers viewModelScope. It cancels automatically when the ViewModel is cleared, so you reuse it.

viewModelScope.launch { load() }

iOS Needs Manual Cancel

On iOS there is no automatic scope. You hold your shared scope and cancel it from Swift when the view disappears. 📱

Never Use GlobalScope

Avoid GlobalScope: it lives forever and ignores lifecycles. Its coroutines leak and may update screens that no longer exist.

Scope It, Then Forget It

Pick the right scope once and cancellation becomes free. Lifecycle-aware scopes keep shared async work safe on both platforms. 🎯

Quick Check

Let us confirm how scopes guard your coroutines.

Recap

You learned that a CoroutineScope ties async work to a lifecycle: launch starts it, cancel cleans it up, and you avoid GlobalScope. 🎉

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

هل درس «CoroutineScope ودورة الحياة» مجاني؟

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

ماذا ستتعلم في «CoroutineScope ودورة الحياة»؟

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

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

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

كم من الوقت يستغرق درس «CoroutineScope ودورة الحياة»؟

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

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

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

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

  1. دوال suspend تعمل في كل مكان
  2. Dispatchers عبر المنصات
  3. CoroutineScope ودورة الحياة
  4. withContext والتزامن المنظم
← العودة إلى Kotlin Multiplatform Academy