我有一个 tableview 应用程序,当用户选择一个视图时,它需要解析一些 XML 以显示信息。但有时 XML 没有完成下载,用户可以按下按钮选择另一个视图,从而产生崩溃。我想我需要取消连接或其他东西不会导致与新连接发生任何冲突,但我不知道具体如何,它应该在 ViewWillDisappear 中正确吗?以下是我在 ViewDidAppear 上启动连接的方式:
NSMutableURLRequest * req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://Adress"]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
conn = [NSURLConnection connectionWithRequest:req delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(conn)
{
receivedData = [[NSMutableData alloc]init];
[DSBezelActivityView newActivityViewForView:self.view withLabel:@"Loading..."];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if(conn) [conn cancel];
}