可以保留 ASIHTTPRequest 委托吗? [英] OK to retain ASIHTTPRequest delegate?
问题描述
是否可以保留 ASIHTTPRequest
的子类的委托?
Is it OK to retain a delegate of a subclass of ASIHTTPRequest
?
我创建了一个名为 JSONRequest
的 ASIHTTPRequest
子类.JSONRequest
的每个实例都是它自己的委托,处理回调,并将它们传递给 jsonDelegate
,这是 JSONRequest
的私有属性,并且响应 requestFinished:withResult:
,其中 result 是 JSON 响应的 NSDictionary
表示.为此,我在 JSONRequest
中重载了 setDelegate:
来做 super.delegate = self;self.jsonDelegate = newDelegate
.
I made a subclass of ASIHTTPRequest
called JSONRequest
. Each instance of JSONRequest
is its own delegate, handles the callbacks, and passes them on to jsonDelegate
, which is a private property of JSONRequest
, and responds to requestFinished:withResult:
, where result is an NSDictionary
representation of the JSON response. To do this, I overloaded setDelegate:
in JSONRequest
to do super.delegate = self; self.jsonDelegate = newDelegate
.
在这种情况下是否可以保留 jsonDelegate
因为通常 jsonDelegate
是一个视图控制器,如果用户点击返回," 等.调用回调方法后,我会在JSONRequest
中释放jsonDelegate
.
Is it OK to retain jsonDelegate
in this case because often jsonDelegate
is a view controller, which is sometimes dealloced while the request is loading if the user hits "Back," etc. I will release jsonDelegate
in JSONRequest
after calling the callback methods.
我怎么知道这是正常的并且不会导致 保留循环.
How do I know this is OK and will not cause a retain loop.
推荐答案
如果你的发布是确定性的(如果你保证它会一直发生,即使是在错误、超时或其他一些意外事件的情况下 -就像连接只是无限期地挂起) - 那么这很好(没有保留循环).
If your release is deterministic (if you promise it will always happen, even in the case of an error, a timeout, or some other sort of unexpected event - like the connection simply hanging indefinitely) - then this is fine (no retain loop).
在这种情况下,保留循环总是会在某个时候被破坏.
In this case, the retain loop would always get broken at some point.
这篇关于可以保留 ASIHTTPRequest 委托吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!