ios - Retain Cycles for Blocks Inside of Blocks -
do have continuously declare weak references break retain cycles blocks inside of blocks?
__weak typeof(self) weakself = self; [self setmyblock:^(id obj, nsuinteger idx, bool *stop) { typeof(self) strongself = weakself; [strongself dosomething]; [strongself setmyblock:^(id obj, nsuinteger idx, bool *stop) { //do need create weak reference strongself block? [strongself dosomething]; }]; }];
i'm afraid so. [strongself setmyblock:<inner block>]
cause self
retain inner block. if inner block has strong reference self
, that's cycle. fact strongself
variable assigned from __weak
qualified variable not make difference.
as other users mentioned, can use original weakself
rather creating new one. reason create strongself
reference in block otherwise self
might deallocated while block running. strongself
either end nil
(if self
deallocated before strongself
assigned, causes no harm) or else self
not deallocated while block executing, because have strong reference. during time have strong reference self
, original weakself
also safe use, because self
not deallocated.
Comments
Post a Comment