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 サーバーからのデータを期待するときに設定されるデフォルトのタイムアウトはありますか。デフォルトのタイムアウトを増やすことはできますか、それともサーバーからデータを受信するまでもう少し待つように指示することはできますか? このタスクを達成する方法を教えてください。御時間ありがとうございます