NSData nil when using NSURLConnection sendSynchronousRequest on iOS8

We have been having an issue with iOS8 where the block of code below is not always populating the NSData object once the sendSynchronousRequest is complete (This fails about 30% of the time) the result is that the NSData to nil.



NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:webStringURL]];

NSURLResponse * response = nil;

NSError * error = nil;

NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest

returningResponse:&response

error:&error];

NSString *dataStr= [NSString stringWithUTF8String:[data bytes]];


The failed and successful responses are identical returning this response in both scenarios:

<NSHTTPURLResponse: 0x16584880> { URL: https://my.url.com/post

"Cache-Control" = "no-store,no-cache,max-age=0,must-revalidate";

Connection = "Keep-Alive";

"Content-Length" = 4;

"Content-Type" = "text/html; charset=ISO8859_1";

Date = "Thu, 14 Jan 2016 16:57:07 GMT";

Expires = "Thu, 01 Jan 1970 00:00:01 GMT";

"Keep-Alive" = "timeout=5, max=100";

Pragma = "no-cache";

Server = Apache;

"Set-Cookie" = "HTJSESSIONID=QXXBWXTDxQTfZkMjJ5J1kLzrvlsgqtbnh5h4pj82s2y7VW2nfFbJ!-1585947779; path=/; secure; HttpOnly";

} }

I have debugged the code and the URL being posted to is giving a valid response with the required data in all scenarios.


In iOS9 this never fails and we have no issues.



Any assistance/ideas would be greatly appreciated.


Thanks,

Greg



1) what do you get for "error" - if it is non-nil then there was an error.

2) stop using synchronous calls - it blocks the thread

3) move over to NSURLSession

NSData nil when using NSURLConnection sendSynchronousRequest on iOS8
 
 
Q