自动释放池和dispatch_async
我读了关于GCD的文章,有一个例子:
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
});
});
如果我将该方法放在单击处理程序中(将在自动释放池中调用),我会丢失stringValue吗,因为单击事件后自动释放池将被销毁?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在那个内部块里面?不,你不会失去这个价值。当在块内引用 Objective-C 对象变量(尚未声明为 __block)并且复制该块时,该对象将自动保留。当块被释放时,该对象也将被释放。
dispatch_async()
负责复制和释放块。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.