Just like the following code:
__weak typeof(self) weakSelf = self;
[self methodThatTakesABlock:^ {
[weakSelf doSomething];
//[self doSomething];//Why not this line?
}];
Just like the following code:
__weak typeof(self) weakSelf = self;
[self methodThatTakesABlock:^ {
[weakSelf doSomething];
//[self doSomething];//Why not this line?
}];
Copyright © 2021 Jogjafile Inc.
Does not cause a retain cycle unless the completion block is stored within
self. If it is a property then,selfwill have a strong reference to the block and the block will have a strong reference toselfcausing a retain cycle. That is why you need to useweak, to avoid this retain cycle. But remember, you must use a weak self only within blocks that are stored as properties or ivars within self.If the completion block is only called in the
methodThatTakesABlockthen you don't have to use a weak self since the block is not retained. In this case, the block will have a strong reference toselfbut self will not have one towards the block, so no retain cycle in this case.