当我允许加载完成时,我有一个NSURLConnection正常工作.但是如果用户点击后退按钮,意味着webview将消失,我想取消正在进行的NSURLConnection.但是如果在调用viewWillDissapear时调用webview,那么我会:
[conn cancel]
我得到一个NSINValidArgument异常.
连接被定义为.h文件中的实例数据:
NSURLConnection *conn;
异步是在这里启动的:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:articleURL]]; if ([NSURLConnection canHandleRequest:request] && reachable) { // Set up an asynchronous load request conn = [NSURLConnection connectionWithRequest:request delegate:self]; loadCancelled = NO; if (conn) { NSLog(@" ARTICLE is REACHABLE!!!!"); self.articleData = [NSMutableData data]; } }
tomute.. 11
你得到异常的原因是你将autorelease对象保存到实例变量.
当用户单击后退按钮时,"conn"将立即自动释放.之后,你打电话取消.因此,你有例外.
要防止这种情况,您应该在将NSURLConnection对象保留在实例变量中时保留它.
conn = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
要么
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
不要忘记在dealloc方法中释放它.
你得到异常的原因是你将autorelease对象保存到实例变量.
当用户单击后退按钮时,"conn"将立即自动释放.之后,你打电话取消.因此,你有例外.
要防止这种情况,您应该在将NSURLConnection对象保留在实例变量中时保留它.
conn = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
要么
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
不要忘记在dealloc方法中释放它.