Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C why need __NSGlobalBlock__

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.

like image 840
ajjnix Avatar asked Oct 31 '22 14:10

ajjnix


1 Answers

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.

like image 75
Avi Avatar answered Nov 15 '22 09:11

Avi