Iphone停止ASIFormDataRequest

xge*_*86x 3 iphone

我有一个问题在我的viewController中,当我有一个待处理的ASIFormDataRequest(作为异步任务启动)仍在执行并且用户按下后退按钮(为了弹出视图).

有没有办法阻止异步任务?

我读过这是一个名为"clearDelegatesAndCancel"的方法,但我不知道它是不是我正在寻找的.

谢谢

Dan*_*Ray 6

clearDelegatesAndCancel事实上,要调用,您必须拥有异步运行的ASIFromDataRequest对象的句柄.这意味着您应该将其设置为属性,例如......

@interface MyViewController : UIViewController <ASIHTTPRequestDelegate>
{
    ASIFormDataRequest *theRequest
    ...
}

@property (nonatomic, retain) ASIFormDataRequest *theRequest;
Run Code Online (Sandbox Code Playgroud)

然后在你的.m中,不要声明一个新的请求对象,只需将你的formdatarequest分配给类的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.
}
Run Code Online (Sandbox Code Playgroud)

重点是,你需要自己设置,以便在异步运行时获得与之交谈的请求.

顺便说一下,这真是一个很好的做法.如果你的viewcontroller在你的请求返回之前消失(比如通过弹出UINavController堆栈),它将尝试在释放的对象上调用delegate方法,然后繁荣.