iOS - NSURLConnection异步请求一次,然后失败(挂起)第二次尝试

时间:2013-04-21 02:52:13

标签: ios asynchronous nsurlconnection

我有一个用户登录我的应用程序的登录视图。 我使用NSURLConnection Asynchrounous Request块连接到我的Web服务并来回传递数据。

我有一个奇怪的问题,如果用户第一次登录,它工作正常。但是如果他们退出,那么尝试再次登录请求被发送但是从不响应(我为所有场景设置了警报视图;即超时,数据= nil,错误!=无等)。它只是在我显示的加载屏幕上,从不做任何事情。

我只是错过了什么?

[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];
         }
     }];

1 个答案:

答案 0 :(得分:0)

好的解决了这个问题。我用来通知我的观点的代理作业是一个问题,我已经完成处理/保存响应数据并执行segue。因为委托是在viewDidLoad中设置的,所以当下一个视图出现并接管委托时,登录视图会丢失委托控件。只需将委托分配移动到viewWillAppear(或viewDidAppear)。