对象超出范围,但使用 ARC 设置了委托
我对这个不明确的标题感到非常抱歉,但我无法很快解释它。
我基本上想知道如果您使用 ARC 并且有这样的方法会发生什么:
- (void)fooMethod:(NSURLRequest *)fooReq
{
NSURLConnection *fooConn = [NSURLConnection connectionWithRequest:fooReq delegate:self];
[fooConn start];
}
So fooConn 在 fooMethod 末尾超出范围,但我仍然会收到回调吗?换句话说:因为没有更多的引用,它会立即被释放(或类似的东西),还是会保留在内存中并处理请求,因为委托仍然设置?
I'm so very sorry for the unclear title, but I can't explain it shortly.
I am basically wondering what happens if you use ARC and you have a method like this:
- (void)fooMethod:(NSURLRequest *)fooReq
{
NSURLConnection *fooConn = [NSURLConnection connectionWithRequest:fooReq delegate:self];
[fooConn start];
}
So fooConn goes out of scope at the end of the fooMethod, but will I still receive callbacks? In other words: because there aren't any more references, will it get dealloc'd (or something like that) immediately, or will it stay in memory and handle the request because the delegate is still set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这完全取决于
NSURLConnection
在幕后做什么。如果保留计数降至零,则 fooConn 仍将被释放,并且我们会假设此时不会再进行回调。现在,我认为幕后的 NSURLConnection 确实会保留在事物层次结构中的某个位置,因此您实际上会收到回调。然而,对于 NSURLConnection ,为了安全起见,我通常会保留对它的强引用。毕竟,作为它的调用者,您希望拥有它,并且希望确保您将继续获得委托回调,而不是受到其幕后发生的任何情况的影响。
所以,简而言之,如果我是你,我会强烈引用它,以确保你不必担心。
That totally depends on what
NSURLConnection
does behind the scenes. If the retain count drops to zero, thenfooConn
will bedealloc
'ed still and one would assume no more callbacks would be made then.Now, I think that
NSURLConnection
behind the scenes does get retained somewhere in the hierarchy of things so you would in fact receive callbacks. However withNSURLConnection
I usually keep a strong reference to it lying around to be on the safe side. After all, you as the caller of it want to own it and you want to ensure that you will keep getting the delegate callbacks and not be at the mercy of whatever happens to it under the hood.So, in short, if I were you I'd keep a strong reference to it to ensure you won't have to worry.
来自 Apple 文档:
连接保留委托。当连接完成加载时释放委托,失败
From the Apple docs:
The connection retains delegate. It releases delegate when the connection finishes loading, fails
fooConn
是一个没有任何所有权限定符的局部变量,因此 ARC 会将其推断为强变量。因此ARC在将connectionWithRequest:delegate:
返回的值存储到foxConn
时会保留它;当退出fooMethod:
时,ARC 将通过释放fooConn
进行清理。委托引用当前实例对此没有影响。
如果您希望
NSURLConnection
在fooMethod:
返回后存在,一种方法是将其存储到实例变量中(ARC 也推断该变量是强的,因此那里也不需要所有权限定符) )。如果在类实例本身不再引用时执行此操作,ARC 将释放NSURLConnection
。fooConn
is a local variable without any ownership qualifier so ARC will infer it as strong. Therefore ARC will retain the value returned byconnectionWithRequest:delegate:
when storing it intofoxConn
; and when exitingfooMethod:
ARC will clean up by releasingfooConn
.That the delegate references the current instance will have no effect on this.
If you wish the
NSURLConnection
to exist afterfooMethod:
returns one way is to store it into an instance variable (which ARC also infers as strong, so no ownership qualifier required there either). If you do this when the class instance itself is no longer reference ARC will release theNSURLConnection
.