ios - objective c - block retain cycle -


I have a block cycle cycle question, suppose I have the following 3 ways in the same class.

  - (zero) foo1WithBlock: (zero (^) (BOOL success)) Complete / Complete // Complete (yes)} - (zero) foo2 {// do something .. } - (zero) foo3 {[Fu 1 with its own block: ^ (Bell's success) {[Fuji Fu 2]; }]; }  

Will Fu create 3 cycles to maintain?

No, no cycle is

However, Own will occupied . This means that the imported will be in the lexical scope of the compound statement (statement executed by the block). It needs to make a copy of "out" variable manually , which creates the block's variable self .

A block can be copied and released . A blockcopy operation will "wrap" the caped variable on the heap. They are there until the block is destroyed through the block block_release operation. The compiler / runtime provides internal functions to copy and release a block and displays them when necessary, for example, if the block was executed with asynchronous via dispatch_async () If the block goes first will be copied , and when the block ends, will be released blockcopy and block_release operations compiler and is executed by runtime, so do not worry.

If the block will be copied, as an effect, will will be retained, and will be issued again when the block is issued - which is when The block ends.

Actually, it guarantees that is valid within the self block and during the life of the block valid (i.e., it will not be deallocated), whether it is called synchronous or asynchronous When the block has been executed asynchronously, the block is copied, and thus self is placed and self continues only after the block is finished Will be done. This also means that the block will extend the "end" to the self lifetime after the block is over.


Comments