NSURLConnection sendAsynchronousRequest wait time in iOS

393 views Asked by At

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

0

There are 0 answers