使用 ARC 手动保留
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我偶尔需要手动保留和释放东西(有时只是为了调试),并想出了以下宏:
这通过使用 __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:
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!
为什么不在异步任务期间将委托对象分配给强 ivar?
或者在
executeAsyncWork
中有一个局部变量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
像这样的事情:
只要需要,该块将保留委托......
Something like this:
The block will retain the delegate as long as needed...