I set the view controller to be the delegate of a local variable ASIHTTPFormDataRequest request
.
But, tapping "Back" before the request has finished, pops and deallocates the view 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.
How do I fix this crash?
One way I could think of solving this is to set the delegate to nil immediately after the navigation controller pops it. But, if that's the solution, how do I do that? (ARC's
weak
references would be sweet right now.)Another way I can think of is to make
request
an instance variable of the view controller and call[request clearDelegatesAndCancel]; [request release];
in the view controller'sdealloc
method. 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. And, this particular view controller is a settings table view controller and has 13 switches. 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!
Thoughts? Ideas?