Question

I have the following call within my app:

 dispatch_async(dispatch_get_main_queue(), ^{
        self.emptyView.hidden = [self.recordsData count];
        [self finishedRefreshing];
    });

which led me to believe that since we are accessing an ivar, a retain cycle would be created. But since I don't see the block being captured within self I figured I'd be ok. This then led me to believe we've been over using the weakself pattern as our typical convention is never to actually assign a block as an ivar. I still feel I'm missing something, are there other ways to create a retain cycle without assigning the block to self? I'm not sure why I'm having such a hard time grasping this concept but any good reading sources are also appreciated.

Was it helpful?

Solution

The "cycle" part does seem to be overlooked when people worry about retain cycles. In your code, the block is managed by GCD, your objects don't own it, so there is no retain cycle.

we've been over using the weakself pattern

It does seem the case that a lot of people think "aargh! Blocks! Make a weakSelf"!

The retain cycles happen when an object owned by the block (so, a strong reference to the object or one of its ivars) itself owns the block (so the block is copied to a property or ivar).

The typical case is when you've made an object such as a custom alert view that has a completion handler block, and within the block you do something with the object itself (e.g. remove the view from its superview).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top