简体   繁体   English

ASIHTTPRequest popViewControllerAnimated: => 委托 EXEC_BAD_ACCESS

[英]ASIHTTPRequest popViewControllerAnimated: => delegate EXEC_BAD_ACCESS

I set the view controller to be the delegate of a local variable ASIHTTPFormDataRequest request .我将视图 controller 设置为局部变量ASIHTTPFormDataRequest request的委托。

But, tapping "Back" before the request has finished, pops and deallocates the view controller.但是,在请求完成之前点击“返回”,弹出并取消分配视图 controller。 So, when the request completes and sends the message -requestDidFinish: to the now nonexistent delegate, the app crashes with an EXEC_BAD_ACECESS exception.因此,当请求完成并将消息-requestDidFinish:发送到现在不存在的委托时,应用程序会因EXEC_BAD_ACECESS异常而崩溃。

How do I fix this crash?如何修复此崩溃?

  1. One way I could think of solving this is to set the delegate to nil immediately after the navigation controller pops it.我能想到的解决这个问题的一种方法是在导航 controller 弹出它后立即将代理设置为 nil。 But, if that's the solution, how do I do that?但是,如果这是解决方案,我该怎么做? (ARC's weak references would be sweet right now.) (ARC 的weak引用现在会很好。)

  2. Another way I can think of is to make request an instance variable of the view controller and call [request clearDelegatesAndCancel]; [request release];我能想到的另一种方法是request视图 controller 的实例变量并调用[request clearDelegatesAndCancel]; [request release]; [request clearDelegatesAndCancel]; [request release]; in the view controller's dealloc method.在视图控制器的dealloc方法中。 This approach is outlined in ASIHTTPRequest Example Code , but I've been advised it's best to make requests local variables instead of instance variables.这种方法在ASIHTTPRequest 示例代码中进行了概述,但我被告知最好使用请求本地变量而不是实例变量。 And, this particular view controller is a settings table view controller and has 13 switches.而且,这个特定的视图 controller 是一个设置表视图 controller 并且有 13 个开关。 For automatic saving to the server, each switch creates & sends a new request each time it's toggled.为了自动保存到服务器,每次切换时,每个开关都会创建并发送一个新请求。 If I made ivars, I'd have to make 13. That's a lot of code!如果我制作了 ivars,我必须制作 13 个。那是很多代码!

Thoughts?想法? Ideas?想法?

I think the first question is: What do you want to happen if the user presses back after pressing a switch?我认为第一个问题是:如果用户在按下开关后按下回,你发生什么? ie. IE。 Should the http request be cancelled, or is it important that the request does get to the server?是否应该取消 http 请求,或者请求到达服务器是否重要? I'll assume for now that you do want to cancel them, as that seems to be implied in your question.我现在假设您确实想取消它们,因为您的问题似乎暗示了这一点。

I've been advised it's best to make requests local variables instead of instance variables我被告知最好是请求本地变量而不是实例变量

I'm not sure if that was good advice - you almost always want requests to not be local variables so you can cope with cases like this.我不确定这是否是个好建议——你几乎总是希望请求不是局部变量,这样你就可以处理这样的情况。

For your case, you could consider using an NSOperationQueue .对于您的情况,您可以考虑使用NSOperationQueue

Rough steps to do this would be:执行此操作的粗略步骤是:

  1. Create an NSOperationQueue in your view controller init.在您的视图 controller init 中创建一个 NSOperationQueue。
  2. When you want to make a http request, add it to the ASIHTTPRequest queue instead of call startAsynchronous当您要发出 http 请求时,将其添加到 ASIHTTPRequest 队列而不是调用 startAsynchronous
  3. In dealloc, iterate the objects in the queue, calling [request clearDelegatesAndCancel];在dealloc中,迭代队列中的对象,调用[request clearDelegatesAndCancel]; for each one, and then release the queue.对于每一个,然后释放队列。

That should solve the crash without needing 13 ivars!这应该可以解决崩溃而不需要 13 个 ivars!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM