NSURLConnection在iOS中发送sendAsynchronousRequest等待时间

时间:2014-01-01 07:37:20

标签: ios7 null nsurlconnection sendasynchronousrequest

我正在使用NSURLConnection sendAsynchronousRequest从Web服务器获取一些数据。问题是有时我能够获取数据,有时会从Web服务器返回NULL。当我查看服务器日志时,它已发送数据。我怀疑这是一个时间问题。以下是我的代码。

 if(netStatus==ReachableViaWiFi || netStatus==ReachableViaWWAN)
 {
    NSLog(@"YES WIFI");

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [NSURL URLWithString:formattedURL];
    [request setURL:[NSURL URLWithString:formattedURL]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *jsonData, NSError *error)
    {
        progressbar.progress = 0.0;

        NSLog(@"Next line of code");

        NSLog(@"dataAsString %@", [NSString stringWithUTF8String:[jsonData bytes]]);

        if ([NSString stringWithUTF8String:[jsonData bytes]]==NULL)
        {
            NSLog(@"R U HERE...THE RESULT IS NULL");
            UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];                
            UIViewController *viewController = [storybord instantiateViewControllerWithIdentifier:@"noserver"];

            viewController.modalPresentationStyle = UIModalPresentationFormSheet;
            [self presentModalViewController:viewController animated:YES];
        }
        else
        {
            searchTable.hidden=FALSE;
            progressbar.hidden=TRUE;
            NSLog(@"R U HERE...THE RESULT IS NOT NULL");
            NSError *error1;
            dictresult = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error1];
            self.searchTable.dataSource=self;
            self.searchTable.delegate=self;
            [searchTable reloadData];
        }

        NSLog(@"Asynch request in search results finished!");
        if (error) {
        } else {
        }
    }];
}

是否存在从Web服务器预期数据时设置的默认超时。我可以增加默认超时,还是可以指示等待一段时间,直到我从服务器收到数据。请让我知道如何完成这项任务。谢谢你的时间

0 个答案:

没有答案