使用 ARC 手动保留

发布于 2024-12-10 11:17:30 字数 311 浏览 0 评论 0原文

在 ARC 之前,我有以下代码,可以在异步操作正在进行时保留委托:

- (void)startAsyncWork
{
    [_delegate retain];
    // calls executeAsyncWork asynchronously
}

- (void)executeAsyncWork
{
    // when finished, calls stopAsyncWork
}

- (void)stopAsyncWork
{
    [_delegate release];
}

ARC 中的此模式等效于什么?

Before ARC I had the following code that retains the delegate while an async operation is in progress:

- (void)startAsyncWork
{
    [_delegate retain];
    // calls executeAsyncWork asynchronously
}

- (void)executeAsyncWork
{
    // when finished, calls stopAsyncWork
}

- (void)stopAsyncWork
{
    [_delegate release];
}

What is the equivalent to this pattern with ARC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无远思近则忧 2024-12-17 11:17:30

我偶尔需要手动保留和释放东西(有时只是为了调试),并想出了以下宏:

#define AntiARCRetain(...) void *retainedThing = (__bridge_retained void *)__VA_ARGS__; retainedThing = retainedThing
#define AntiARCRelease(...) void *retainedThing = (__bridge void *) __VA_ARGS__; id unretainedThing = (__bridge_transfer id)retainedThing; unretainedThing = nil

这通过使用 __bridge_retained 和 __bridge_transfer 将东西转换为 (void *) 来实现,这会导致东西被保留,或者创建强引用而不调用保留。

玩得开心,但要小心!

I have occasionally needed to manually retain and release things (sometimes just for debugging) and came up with the following macros:

#define AntiARCRetain(...) void *retainedThing = (__bridge_retained void *)__VA_ARGS__; retainedThing = retainedThing
#define AntiARCRelease(...) void *retainedThing = (__bridge void *) __VA_ARGS__; id unretainedThing = (__bridge_transfer id)retainedThing; unretainedThing = nil

This works by using the __bridge_retained and __bridge_transfer to cast things to and from (void *) which causes things to be retained, or to create a strong reference without calling retain.

Have fun, but be careful!

幸福丶如此 2024-12-17 11:17:30

为什么不在异步任务期间将委托对象分配给强 ivar?

或者在 executeAsyncWork 中有一个局部变量

- (void)executeAsyncWork
{
    id localCopy = _delegate;

    if (localCopy != nil) // since this method is async, the delegate might have gone
    {
        // do work on local copy
    }
}

Why not just assign your delegate object to a strong ivar for the duration of the asynchronous task?

Or have a local variable in executeAsyncWork

- (void)executeAsyncWork
{
    id localCopy = _delegate;

    if (localCopy != nil) // since this method is async, the delegate might have gone
    {
        // do work on local copy
    }
}
留一抹残留的笑 2024-12-17 11:17:30

像这样的事情:

- (void)startAsyncWork
{
    id<YourProtocol> delegate = _delegate;
    dispatch_async(/* some queue */, ^{
        // do work
        [delegate doSomething];
    }
}

只要需要,该块将保留委托......

Something like this:

- (void)startAsyncWork
{
    id<YourProtocol> delegate = _delegate;
    dispatch_async(/* some queue */, ^{
        // do work
        [delegate doSomething];
    }
}

The block will retain the delegate as long as needed...

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