Should I use weakSelf in nested blocks?
<p>I'm trying to correctly avoid retain cycles with blocks in Objective C, and am not sure about having nested blocks.</p>

<p>If I write a simple block like this:</p>

<pre><code>[self doSomethingWithBlock:^{
    [self doSomethingElse];
}];
</code></pre>

<p>The compiler catches and warns me that this could cause retain cycles. I change it as follows to avoid the cycle:</p>

<pre><code>__weak __typeof(self)weakSelf = self;
[self doSomethingWithBlock:^{
    __strong __typeof(weakSelf)strongSelf = weakSelf;
    [strongSelf doSomethingElse];
}];
</code></pre>

<p>When I write something like this:</p>

<pre><code>[self doSomethingWithBlock:^(MyObject* object){
    [object doSomethingElseWithBlock:^{
        [self doYetAnotherThing];
    }];
}];
</code></pre>

<p>The compiler is happy, but I'm not convinced that it's safe. Even though there is an intermediary <code>object</code> in between, it still looks conceptually the same as above, but now it's a cycle with 3 retains.</p>

<p>Should it be like this instead?</p>

<pre><code>[self doSomethingWithBlock:^(MyObject* object){
    __weak __typeof(self)weakSelf = self;
    [object doSomethingElseWithBlock:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf doYetAnotherThing];
    }];
}];
</code></pre>

<p>Or like this?</p>

<pre><code>__weak __typeof(self)weakSelf = self;
[self doSomethingWithBlock:^(MyObject* object){
    [object doSomethingElseWithBlock:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf doYetAnotherThing];
    }];
}];
</code></pre>