I read the article about GCD, and there is an example:
dispatch_queue_t bgQueue = myQueue;
dispatch_async(dispatch_get_main_queue(), ^{
NSString *stringValue = [[[textField stringValue] copy] autorelease];
dispatch_async(bgQueue, ^{
// use stringValue in the background now
});
});
If i place that method in click handler (which will be called in the autoreleasepool), will i loss stringValue, because autoreleasepool will be destroyed after click event?
Inside that inner block? No, you won’t lose that value. When an Objective-C object variable (which hasn’t been declared as
__block) is referenced inside a block and the block is copied, that object is automatically retained. When the block is released, that object will be released, too.dispatch_async()is responsible for copying and releasing the block.