I'm doing some network requests using [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:completionHandler]
.
Unfortunately, half the time I perform these requests, I get an NSError
with A server with the specified hostname could not be found
. I also get Request timed out
some of the time, but less frequently.
I'm in a basement, so it could just be an issue with network connectivity, but I'm able to send requests just fine with other apps. I'm also able to load the URL just fine in safari and speed tests seem fine.
Requests are sent to over http
. There are also times when 2 or 3 requests may be sent at the same time.
My questions:
- Does
sendAsynchronousRequest
do anything significantly different from the delegate basedNSURLRequest
methods that would be causing this behavior? - If not, are there preferred ways of gracefully handling these errors? I've tried just retrying the request up to a limit, but they all result in the error. I've thought about setting a delay between retries, but it doesn't seem to make a difference.
Have you tried out Apple's reachability wrapper? It may help you isolate bugs. If Reachability says one thing but NSURLConnection is saying something else, there may be something wrong. I'm under the assumption that the webservice you're querying is under too much load, or your internet is flaky.
http://developer.apple.com/library/IOs/samplecode/Reachability/Introduction/Intro.html
You can use the Reachability.h/.m class to query the system for Internet reachability, by testing Internet connectivity altogether as well as hostname connectivity.