0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · درس

كسر دورات الاحتفاظ في Blocks

شخّص وأصلح أكثر تسريبات الذاكرة شيوعًا في التطبيقات القديمة التي تستخدم ARC: دورات الاحتفاظ التي تسببها Blocks عند التقاط self، باستخدام مراجع weak وstrong.

كسر دورات الاحتفاظ في Blocks درس مجاني في Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Objective-C iOS Development for Legacy & Enterprise Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.

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

What is a Retain Cycle?

A retain cycle happens when two objects strongly reference each other. Neither's count can reach zero, so neither is ever freed — a memory leak ARC cannot break for you.

Blocks Capture Strongly

An Objective-C block captures the variables it uses, including self — and it captures them strongly by default. This is the number one source of cycles.

A Cycle in Action

Here self owns the block (stored in a property), and the block strongly captures self. That mutual ownership leaks.

self.completion = ^{
  [self refreshUI];
};

The weak Qualifier

Break the cycle by capturing a weak reference to self. A weak reference does not increase the retain count.

__weak typeof(self) weakSelf = self;
self.completion = ^{
  [weakSelf refreshUI];
};

The weakSelf Pattern

The convention names the capture weakSelf using __weak typeof(self) so the type stays correct even if the class changes.

__weak typeof(self) weakSelf = self;

weakSelf Can Become nil

A weak reference becomes nil the instant the object deallocates. If the block runs after that, messages to weakSelf are silently ignored — but partial access can be inconsistent.

The strongSelf Guard

Inside the block, promote weakSelf to a temporary strong reference so it cannot vanish mid-execution.

self.completion = ^{
  __strong typeof(weakSelf) strongSelf = weakSelf;
  if (strongSelf) {
    [strongSelf refreshUI];
  }
};

When You Do NOT Need weakSelf

Not every block leaks. If self does not own the block — for example a one-shot block passed to a method and not stored — capturing self strongly is fine and even safer.

[UIView animateWithDuration:0.3 animations:^{
  self.view.alpha = 0;
}];

Delegate Cycles

The same rule applies to delegates: a delegate property should be weak so a view controller and its child do not retain each other.

@property (nonatomic, weak) id<MyDelegate> delegate;

Finding Leaks

Tools to hunt cycles:

  • Instruments > Leaks flags leaked allocations
  • Memory Graph Debugger in Xcode visualizes retain cycles
  • Log in dealloc to confirm objects are freed

Rule of Thumb

Ask: does self own this block, and does the block touch self? If both are yes, use weakSelf (plus a strongSelf guard for multi-step work).

Quick Check

Test your retain cycle knowledge.

Recap

You learned to break retain cycles:

  • Blocks capture self strongly by default
  • A cycle leaks when self owns a block that references self
  • Use __weak typeof(self) weakSelf to break it
  • Add a strongSelf guard for multi-step work
  • Mark delegate properties weak

Instruments and the Memory Graph confirm your fixes.

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

هل درس «كسر دورات الاحتفاظ في Blocks» مجاني؟

نعم — نص درس «كسر دورات الاحتفاظ في Blocks» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Objective-C iOS Development for Legacy & Enterprise Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.

ماذا ستتعلم في «كسر دورات الاحتفاظ في Blocks»؟

شخّص وأصلح أكثر تسريبات الذاكرة شيوعًا في التطبيقات القديمة التي تستخدم ARC: دورات الاحتفاظ التي تسببها Blocks عند التقاط self، باستخدام مراجع weak وstrong. تتمرن على Objective-C iOS Development for Legacy & Enterprise Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Objective-C iOS Development for Legacy & Enterprise Apps؟

لا تُشترط خبرة سابقة. Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «كسر دورات الاحتفاظ في Blocks»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Objective-C iOS Development for Legacy & Enterprise Apps هذا؟

نعم. كل درس في Objective-C iOS Development for Legacy & Enterprise Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. أساسيات Manual Retain-Release (MRR)
  2. العدّ التلقائي للمراجع (ARC)
  3. المراجع الضعيفة مقابل القوية
  4. كسر دورات الاحتفاظ في Blocks
← العودة إلى Objective-C iOS Development for Legacy & Enterprise Apps