在没有实例变量的情况下删除 dealloc 上的委托
所以我在 UIViewController 中的 [viewDidLoad] 上启动 ASIFormDataRequest 。
ASIFormDataRequest *detailRequest = [ASIFormDataRequest requestWithURL:url];
detailRequest.delegate = self;
[detailRequest startAsynchronous];
如果我的 UIViewController 在我的请求完成之前被释放,我的应用程序就会崩溃。
例如,如果我将 ASIFormDataRequest 添加为实例变量
@property(nonatomic, retain) ASIFormDataRequest *detailRequest;
,并将 dealloc 上的委托设为 nil,
-(void)dealloc {
if(self.detailRequest != nil) { self.detailRequest.delegate = nil; }
self.detailRequest = nil;
[super dealloc];
}
则应用程序将不再崩溃。
但我认为没有必要为此创建一个实例变量,特别是如果我有多个请求。
有更好的方法吗?
so i start a ASIFormDataRequest on my [viewDidLoad] in a UIViewController.
ASIFormDataRequest *detailRequest = [ASIFormDataRequest requestWithURL:url];
detailRequest.delegate = self;
[detailRequest startAsynchronous];
If my UIViewController gets released before my Request finishes, my app crashes.
If i add my ASIFormDataRequest as an instance variable for example
@property(nonatomic, retain) ASIFormDataRequest *detailRequest;
and nil the delegate on dealloc
-(void)dealloc {
if(self.detailRequest != nil) { self.detailRequest.delegate = nil; }
self.detailRequest = nil;
[super dealloc];
}
the app no longer crashes.
but i don't think it's necessary to create a instance variable just for this, especially if i have multiple requests.
is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通常创建一个数组并将所有活动请求存储在该数组中。当请求完成时,我删除该请求,当控制器调用 dealloc 时,我取消所有请求并将委托清零。
I usually create an array and store all active requests in the array. When the request is completed I remove the request, and when the controller calls dealloc I cancel all of the requests and nil the delegate.
为了释放它,你必须有一个指向它的指针,所以是的,使用 ivar。 iars并不贵。
In order to release it you must have a pointer to it so yes, use an ivar. iars are not expensive.
通过执行
self.detailRequest = [ASIFormDataRequest requestWithURL:url];
我猜测它正在创建一个autorelease
对象,其生命周期未绑定到您的控制器类。如果对象的创建和删除绑定到控制器,则使用实例变量是合乎逻辑的。有关自动释放的更多详细信息
By doing
self.detailRequest = [ASIFormDataRequest requestWithURL:url];
I am guessing it is creating anautorelease
object whose lifespan isn't bound to your controller class. If the creation and deletion of your object is bound to your controller, it's logical to use a instance variable.More details about autorelease
您可以这样做:
然后调用
ASIFormDataRequest 回调方法。无论如何,这就是我通常倾向于做的事情。
这样,请求对象在请求期间保留其委托。
You could do this:
and then call
In the ASIFormDataRequest callback method. That's what I generally tend to do, anyway.
That way, the request object retains its delegate for the duration of the request.
由于这是异步请求,因此如果您设置委托,则意味着一旦响应到来,您的委托方法就会被调用。到那时你的对象应该还活着来处理响应。因此,在 dealloc 中保留并释放它就可以了,在此之前,您必须将 delegate 设置为 nil。这样,如果在释放方法后有响应,框架不应该被误导去寻找死亡对象的方法。
要处理多个请求,最好的方法是创建要使用的数组和对象数量。当你处理完对象后,在 dealloc 方法中迭代每个对象并设置 delegate nil 并释放该对象。
As this is the Asynchronous request so if you set delegate it means as soon as response comes your delegate methods will be called. Till that time your object should be alive to handle the response. So making it retain and releasing in the dealloc is fine and before than that you have to set delegate to nil. So that if response comes after releasing the method, framework should not be misguided to search for method of dead object.
To handle multiple request the best way is to create the array and number of objects you want to use. When you are done with the objects, in dealloc method iterate through each object and set delegate nil and release the object.