The intent here is two-fold:
First, is the use of:
__weak __typeof(self) wself = self;
This ensures that the cmd
block does not maintain a strong reference to self
. This ensures that, if cmd
was an instance variable of the class, that you don't end up with a strong reference cycle. If you don't use this wself
pattern, the class that has cmd
as an instance variable would never be released and you'd leak the object that has cmd
as an instance variable.
For more information, see the Avoid Strong Reference Cycles when Capturing self section of the Programming with Objective-C: Working With Blocks document.
Second, the use of the following within the block:
__strong __typeof(self) sself = wself;
if (!sself) return;
This ensures that, if the block starts execution, if wself
was already deallocated, the block would quit. But if wself
has not yet been deallocated, by assigning sself
, you're making sure that the object will be retained during the execution of the block.
Also, if you reference any ivars in the block, be aware that you want to dereference them (because otherwise there is an implicit reference to self
in the block, potentially leading to that strong reference cycle). But you cannot dereference ivars using a weak pointer (e.g. wself->someIvar
is not permitted), but you can with this local strong pointer (e.g. sself->someIvar
is ok). Generally you shouldn't be dereferencing ivars anyway, but rather use properties, but nonetheless it's another reason to use a local strong reference to self
.
Often, you'll see this construct in conjunction with a class property (or an ivar):
@property (nonatomic, copy) void (^commandBlock)(void);
And, as a matter of convention, you'll generally see more descriptive variable names, weakSelf
and strongSelf
, thus:
__weak __typeof(self) weakSelf = self;
self.commandBlock = ^(){
__strong __typeof(self) strongSelf = weakSelf;
if (!strongSelf) return;
...
};
This weakSelf
/strongSelf
pattern is very common when you have your own block properties to your class and you want to (a) prevent strong reference cycles (aka retain cycles); but (b) want to ensure that the object in question cannot be deallocated in the middle of the execution of the block.