The question is really worded incorrectly. This is not โwhen do I need __block?โ, It is โwhat does __block do?โ. Once you understand what he is doing, you can tell when you need it.
Usually, when a block captures a variable (capture occurs when the block refers to a variable outside of itself), it creates a copy of the variable (note that in the case of objects, a copy of the pointer is created, not the object itself), and if it is an object, it saves it.
This means that in normal behavior, you cannot use a block to change a value outside the block. This code is invalid, for example:
int x = 5; void(^block)() = ^{ x = 10; };
The __block qualifier makes two changes: most importantly, it tells the compiler that the block should capture it directly, and not make a copy. This means that you can update the value of variables outside the block. Less important, but still very important, when not using ARC, it tells the compiler not to save the captured objects.
Catfish_man
source share