2

I am using NSURLConnection sendAsynchronousRequest to fetch some data from the web server. The issue is sometimes I am able to fetch the data and some times NULL is returned from the web server. When I looked in to the server logs, it has sent the data. I suspect it as a timing issue. Below is my code.

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

Is there a default timeout which is set when expecting data from the web server. Can I increase the default timeout or can I instruct to wait for some more time till I receive the data from the server. Please let me know how to accomplish this task. Thanks for your time

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • Please let me know if I am missing any info in my question – Timothy Rajan Jan 01 '14 at 08:52
  • After further investigation, I got to know this is not a issue related to timing. Because, I am able to see the data sent from server to the app. What is confusing me is 8 out of 10 times, I am getting the data in the app. Only 2 times out of 10 times, I am getting NULL even though the data is being sent from the server. Any pointers will be really helpful – Timothy Rajan Jan 01 '14 at 13:20
  • Do you get some error from `NSError *error` when you get nil of jsonData? – KudoCC Jan 03 '14 at 08:01
  • @KudoCC No. There is no error. the value is NULL. I am not seeing a time out issue also. Totally confused. – Timothy Rajan Jan 06 '14 at 23:15

0 Answers0