Blocks, self, save cycles

I am having problems so that I can refer to myself inside the block, and not to create a save loop.

Could you tell me if I understand correctly:

If I ever reference self inside a block, it will create a save loop, and instead I have to create a weak reference to self outside the block, and then use this weak reference inside the block?

Thank!

+5
source share
1 answer

Yes, this is correct, with a few exceptions:

Saving a loop occurs only if it selfends by holding the block indirectly, for example. property myblockby property self myproperty:

self.myproperty.myblock = ^{ [self dosomething]; }; // ERROR: Retain Cycle

() - , :

dispatch_async(dispatch_get_main_queue(), ^{ [self dosomething]; }); // Safe, dispatch_async will not be retained by you

, , dispatch_async , .

, , . , :


, pre-ARC , , , .

, , , self type be a __weak instancetype const instancetype const. ARC, , , , .

, .

+11

All Articles