iOS - NSURLConnection异步请求一次,然后失败(挂起)第二次尝试
问题描述:
我有一个登录视图,用户将用于登录到我的应用程序。 我使用NSURLConnection Asynchrounous请求块连接到我的Web服务并来回传递数据。iOS - NSURLConnection异步请求一次,然后失败(挂起)第二次尝试
我有一个奇怪的问题,如果用户第一次登录,它工作正常。但是,如果他们注销,然后尝试再次登录请求发送,但从来没有响应(我设置所有场景的alertviews;即超时,数据=无,错误!=零等)。它只是与我显示的加载屏幕坐在那里,并从不做任何事情。
我是否错过了什么?
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil){
if(data != nil) {
//Handle Data Here
.............
} else {
[loadingView removeFromSuperview];
loadingView = nil;
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"Network connection was successful, but there was a problem with data transfer.\nPlease try again."];
[alert setCancelButtonWithTitle:@"OK" block:nil];
[alert show]; }
}
else if ([data length] == 0 && error == nil){
[loadingView removeFromSuperview];
loadingView = nil;
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"Network connection was successful, but there was a problem with data transfer.\nPlease try again."];
[alert setCancelButtonWithTitle:@"OK" block:nil];
[alert show];
}
else if (error != nil && error.code == NSURLErrorTimedOut){
[loadingView removeFromSuperview];
loadingView = nil;
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"The network request timed out.\nPlease check your internet connection and try again."];
[alert setCancelButtonWithTitle:@"OK" block:nil];
[alert show];
}
else if (error != nil){
[loadingView removeFromSuperview];
loadingView = nil;
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"There was a problem connecting to the network.\nPlease check your internet connection and try again."];
[alert setCancelButtonWithTitle:@"OK" block:nil];
[alert show];
}
}];
答
好的解决了这个问题。与我正在使用的委托指派有关的问题是,我通知我的观点,我完成了处理/保存响应数据并执行赛格。由于代理正在viewDidLoad中设置,当下一个视图出现并接管委托时,登录视图失去了委托控制。只需将委托任务移至viewWillAppear(或viewDidAppear)。