i have a problem in my viewController when i have a pending ASIFormDataRequest (started as an asynchronous task) that is still executing and the user presses the back button (in order to pop the view).
Is there any way to stop that asynchronous task?
I have read that is a method called "clearDelegatesAndCancel" but i don't know if it is what i'm looking for.
Thanks
Thing is, to call clearDelegatesAndCancel, you have to have a handle to the ASIFromDataRequest object that's running asynchronously. That means you should set it up as an property, like...
@interface MyViewController : UIViewController <ASIHTTPRequestDelegate>
{
ASIFormDataRequest *theRequest
...
}
@property (nonatomic, retain) ASIFormDataRequest *theRequest;
Then in your .m, don't declare a new request object, just assign your formdatarequest to the class's iVar:
@synthesize theRequest;
-(void)viewDidLoad //or whatever
{
self.theRequest = [ASIFormDataRequest requestWithUrl:myUrl];
// then configure and fire the request, being sure to set .delegate to self
}
-(void)viewWillDisappear:(BOOL)animated //or whatever
{
[self.theRequest clearDelegatesAndCancel];
}
-(void)dealloc
{
[theRequest release]; //don't not do this.
}
Point is, you need to set yourself up so that you've GOT the request to talk to while it's running asynchronously.
By the way, this is REALLY good practice. If your viewcontroller goes away (say by getting popped off the UINavController stack) before your request returns, it'll try to call the delegate method on a deallocated object, and boom.
From ASI docs (http://allseeing-i.com/ASIHTTPRequest/How-to-use)
To cancel an asynchronous request (either a request that was started with
[request startAsynchronous] or a request running in a queue you created),
call [request cancel]. Note that you cannot cancel a synchronous request.
Note that when you cancel a request, the request will treat that as an error,
and will call your delegate and/or queue’s failure delegate method. If you do
not want this behaviour, set your delegate to nil before calling cancel, or
use the clearDelegatesAndCancel method instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With