If my block don't capture variables I get __NSGlobalBlock__
.
Class class = [^{
} class];
NSLog(@"%@", NSStringFromClass(class));
But if I capture variables I get __NSStackBlock__
int foo = 3;
Class class = [^{
int foo1 = foo;
} class];
NSLog(@"%@", NSStringFromClass(class));
Why need block in global memory?
What advantage __NSGlobalBlock__
vs __NSStackBlock__
?
I read Block Implementation Specification, but I don't understand why need __NSGlobalBlock__
if I create block for only one usage.
Global
and Stack
refer to where the captured data resides at the time the block is defined. If a block is Global
the runtime knows that no further processing will need to be done. Things like copy
become a no-op. If the block is Stack
, then the runtime is aware that the data needs to be moved if it will go out of scope before the block is released. This is especially important for ARC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With