You should not grab something weak because you get a warning from the compiler; compiler warning just wonders; he does not know how the methods you call make references. You should only do this if you understand the link architecture and determine that a loop exists and determine that capturing a weak link instead preserves intentional behavior. You did not give us the -doSomethingWithBlock: code. This would create a save loop if inside this method it assigns a block to a property variable or an instance of self . Does it do it or not? If not, then there is no hold cycle, and it makes no sense that the external block does not commit self .
Assuming that the capture of the external block of self is weak, examples in which the external block of capture of self out of the question. The rest of the questions are whether the internal block should capture self (or any other version of self , such as strongSelf ). In other words, will you do something like this:
__weak __typeof(self) weakSelf = self; [self doSomethingWithBlock:^(MyObject* object){ __strong __typeof(weakSelf) strongSelf = weakSelf; if (strongSelf) { [object doSomethingElseWithBlock:^{ [strongSelf doYetAnotherThing]; }]; } }];
or something like this:
__weak __typeof(self) weakSelf = self; [self doSomethingWithBlock:^(MyObject* object){ __strong __typeof(weakSelf) strongSelf = weakSelf; if (strongSelf) { [object doSomethingElseWithBlock:^{ __strong __typeof(weakSelf) strongSelf = weakSelf; if (strongSelf) { [strongSelf doYetAnotherThing]; } }]; } }];
Again, the main problem to determine is whether there is a save cycle if the inner block strongly fixes self . There will only be a save loop if [object doSomethingElseWithBlock:... somehow assigns the block to a property variable or an instance of self . But how could this be? The method is called on object , not self . The method does not get self any way. Unless something complicated happens, the method will not assign a variable to the property or instance self , so it is unlikely that a save loop will be created. This means that locking the inner self block is not weakly required to prevent a save cycle.
But whether the internal block can fix self weakly or strongly affect behavior. Namely, if the inner block weakly fixes self , self can be released by the time the block starts, in which case [strongSelf doYetAnotherThing]; will not be executed, whereas if the internal block captured self strongly, it would keep self live and [strongSelf doYetAnotherThing]; will be executed. So it depends on what -doYetAnotherThing does. If it performs some kind of user interface operation on self , which is a user interface representation or something like that, then whether you perform it in a view that is no longer displayed does not matter. But if he, for example, sends something to the network or something like that, then regardless of whether it is being executed, it can make a big difference.
source share