Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a reachability timeout on ios

I use the Reachability class to know if I have an internet connection available. The problem is when wifi is available but not internet, the - (NetworkStatus) currentReachabilityStatus method take too much time.

my code:

Reachability* reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

The application "freeze" temporarily on the second line. How to define the maximum time for this waiting ?

like image 692
Anthony Avatar asked Apr 11 '12 11:04

Anthony


1 Answers

I don't think so. But more importantly, I don't think you'd want to if you could (you may get false positives). Let Reachability run it's course.

If you look at the Reachability demo project, the notion isn't to invoke reachabilityWithHostName and check currentReachabilityStatus when you need the Internet. You invoke currentReachabilityStatus at during your app delegate's didFinishLaunchingWithOptions, set up a notification, and Reachability will tell you when the Internet connectivity has changed. I find that subsequent checks to currentReachabilityStatus are plenty fast (regardless of connectivity) when I (a) setup reachability at startup; but (b) check for connectivity in a just-in-time manner.

And if you absolutely need to start your processing immediately, then the question is whether you can push that into the background (e.g. dispatch_async()). E.g., my app retrieves updates from the server, but because that's happening in the background, neither me nor my user are aware of any delays.

like image 164
Rob Avatar answered Nov 07 '22 21:11

Rob