When to use the __block keyword to reference objects using ARC

I understand that the __block storage __block needed for scalar variables so that the block can see updates for them, but when is it needed for objects? I believe that __weak should be used when writing self-promotion, which will be used inside the block, but I do not see when it would be necessary to use the __block storage __block for ordinary objects.

+6
source share
2 answers

__block needed for scalar variables if you want to change their value using code inside the block. Captured scalars are displayed as const inside the block and therefore cannot be changed. If you have a pointer to an object, the same difference applies - the captured pointer itself will be a const pointer and therefore cannot be changed, but the specified object can be changed by code inside the block. If you want to change the object that it points to, then the pointer itself must change, and therefore the pointer must be declared with the __block type. You never need to declare the object itself as __block , but only a pointer to the object, and only if the pointer needs to be changed.

If you have the right mental model, the blocks are not so confusing. It is important to know that the blocks are initially allocated on the stack and therefore disappear when the lexical region is destroyed as the stack frame stacks. If you want the block to freeze during the lifetime of the lexical area in which the block was created, move it to the heap using Block_copy() or send the Block_copy() message. When a block is copied to the heap, all captured const variables go, and all objects specified by these const variables are saved. When a block is removed from the heap, all objects pointed to by const variables are freed.

__block variables โ€œunder the hoodโ€ have an additional layer of indirection used by the compiler (and which you donโ€™t see) included in the block, so when the block is copied to the heap, t20> and the invisible pointers are configured to point to the new location of the heap of these variables __block . This means that the address of the __block variable can change, so be careful if you use this address. You can also see that the __block variable lives in a sense โ€œoutsideโ€ the block, so these variables can be read and modified from code external to the block.

I was brief, but here you can find the best explanations listed in increasing difficulty:

http://ios-blog.co.uk/tutorials/programming-with-blocks-an-overview/

http://www.cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html

http://www.mikeash.com/pyblog/friday-qa-2011-06-03-objective-c-blocks-vs-c0x-lambdas-fight.html

+15
source

They are used for function level variables. They change inside the block (and the enclosing area) and are saved if any reference block is copied to the heap. Variables local to the covering lexical region declared using the __block repository __block are provided by reference and are therefore mutable. Any changes are reflected in the covering lexical area, including any other blocks defined within the same covering lexical area.

__block variables live in a repository that is shared between the lexical region of the variable and all blocks and block copies declared or created within the lexical region of the variables. Thus, the storage will be preserved upon destruction of the stack frame if any copies of blocks declared within the frame go beyond the frame (for example, by placing it somewhere for later execution). Therefore, use them when you need to change an object inside a block or when you need an object after destroying a stack frame.

0
source

Source: https://habr.com/ru/post/950135/


All Articles