自动释放池和dispatch_async

发布于 2024-12-12 12:41:55 字数 370 浏览 7 评论 0原文

我读了关于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-12-19 12:41:55

在那个内部块里面?不,你不会失去这个价值。当在块内引用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文