手动保留ARC [英] Manual retain with ARC
本文介绍了手动保留ARC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在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];
}
ARC的这种模式相当于什么?
What is the equivalent to this pattern with ARC?
推荐答案
为什么不在异步任务期间将你的委托对象分配给强大的ivar?
Why not just assign your delegate object to a strong ivar for the duration of the asynchronous task?
或者在中有一个局部变量executeAsyncWork
- (void)executeAsyncWork
{
id localCopy = _delegate;
if (localCopy != nil) // since this method is async, the delegate might have gone
{
// do work on local copy
}
}
这篇关于手动保留ARC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文