>
> On 09.07.2012, at 18:03, Fritz Anderson wrote:
>
>> You can break this by having a strong reference to self that the block
>> can manage independently.
>>
>>   __block MyClass *      blockSelf = self;
>>   [self.operationQueue addOperationWithBlock:^{
>>       [blockSelf bar];
>>       blockSelf = nil;
>>   }];
>
> Thank you Fritz for your answer.
>
> But if I break the cycle and if I understand it correctly, 'self' won't be
> retained anymore within the block. If there are no more references to
> 'self' (except one possibly within the block), I fear 'self' will be
> destroyed before the block executes. I do require, though, that 'self'
> will be valid as long as the block is not finished.
>
>
>
> Note: I'm using ARC in which case a retainable pointer declared with
> __block will be retained (as opposed to manual ref counting), so I should
> use __weak or __unsafe_unretained instead of __block to break the cycle
> (if that is what I have to do).
>
> Andreas

No, if you use the syntax Fritz suggests and there is no need to fear self
being destroyed before the block is finished. This is also in the
Transitioning To ARC release notes.

blockSelf is retained by the block, since blockSelf just points to self,
that retains self. At the end of the block, but not before, you're setting
it to nil, which gets rid of that strong reference. All this little trick
really does is 'rename' self to something else so you can manage its
lifecycle.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to