Save loops for blocks inside blocks

Should I constantly declare weak links to interrupt block hold cycles within 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 I need to create another weak reference to strongSelf for this block?
        [strongSelf doSomething];
    }];  
}];
+4
source share
1 answer

I'm afraid. [strongSelf setMyBlock:<inner block>]will cause the selfindoor unit to be retained. If the inner block has a strong reference to self, this is a loop. The fact that a variable strongSelfwas __weakoriginally assigned from a qualified variable does not matter.

, weakSelf . , strongSelf , , self . strongSelf nil ( self strongSelf, ), self , . , self, weakSelf , self .

+6

All Articles