当我有一个仍在执行的挂起的ASIFormDataRequest (作为异步任务启动),并且用户按下back按钮(为了弹出视图)时,我的viewController出现了问题。
有什么方法可以停止这个异步任务吗?
我读到过一种叫做"clearDelegatesAndCancel“的方法,但我不知道它是否是我要找的。
谢谢
发布于 2011-01-25 23:56:11
问题是,要调用clearDelegatesAndCancel,您必须拥有异步运行的ASIFromDataRequest对象的句柄。这意味着你应该把它设置为一个属性,就像...
@interface MyViewController : UIViewController <ASIHTTPRequestDelegate>
{
ASIFormDataRequest *theRequest
...
}
@property (nonatomic, retain) ASIFormDataRequest *theRequest;然后在你的.m中,不要声明一个新的request对象,只需将你的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.
}重点是,您需要对自己进行设置,以便在异步运行时有与之对话的请求。
顺便说一下,这真的是一个很好的实践。如果你的视图控制器在你的请求返回之前离开了(比如从UINavController堆栈中弹出),它将尝试调用一个已释放的对象上的委托方法,然后轰隆一声。
https://stackoverflow.com/questions/4795233
复制相似问题