I did get used to write
__weak typeof( self ) wself = self
[smth doSomeBlock:^(void) {
__strong typeof( wself ) sself = wself;
[sself callAny];
}]
So i avoid retain cycles
But what to do when multiple blocks are nested?
__weak typeof( self ) wself = self
[smth doSomeBlock:^(void) {
__strong typeof( wself ) sself = wself;
[sself doSomeBlock:^(void) {
// wself, or sself? my mind is blown
// what if another block?
}];
}]
Your first example looks like you're wasting your effort. There's no problematic retain cycle unless: self
owns smth
AND smth
owns the Block AND you're not going to destroy the Block until self
is destroyed. If any of those is not true, there's no reason to do the weak/strong dance.
The second situation is the same: the nesting doesn't change anything. Is there a chain of ownership from self
to the Block or not? If no, then it doesn't matter which class of pointer you use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.