An important aspect of the original question could be further clarified. "When I pass self
as an argument to a function, does it make any difference if I weakify it first?"
Note that in the example code block:
__weak __typeof(self) weakSelf = self;
[self.someObject doWorkWithDelegate: weakSelf];
ARC will perform a retain on each of the objects passed, including the receiver (i.e. self.someObject
and weakSelf
), and then a release on each when complete, allowing weak objects to be safely used for the lifetime of the called method. So the answer would be no, it doesn't make a difference to make it weak first because it will be strong for the duration of the call.
If the weak variable was simply referenced within a code block callback from the method, then it will still be weak within that scope.
So this is a common pattern to use a weakSelf
variable within a block callback to call another method, foo
, which then can safely use self
because it is no longer weak during that execution of foo
. But if self
goes away before the callback is executed, then [weakSelf foo]
will simply never be called because weakSelf
has gone nil.
__weak __typeof(self) weakSelf = self;
[self.someObject doWorkWithCallback:^{
[weakSelf foo];
}];
- (void)foo
{
// Safely use self here.
[self doMoreWork];
}