How to determine reachability timeout on ios

I use the Reachability class to find out if I have access to the Internet. The problem is that Wi-Fi is available, but not the Internet, the method - (NetworkStatus) currentReachabilityStatus takes too much time.

my code is:

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

The application is temporarily "frozen" in the second line. How to determine the maximum latency?

+6
source share
2 answers

I do not think so. But more importantly, I don’t think you want it if you can (you can get false positives). Let Rachability complete the course.

If you look at the Reachability demo project, the concept should not call reachabilityWithHostName and check the currentReachabilityStatus when you need the Internet. You call currentReachabilityStatus while you delegated your application didFinishLaunchingWithOptions, set up a notification, and Reachability tells you when your Internet connection has changed. I found that subsequent checks on currentReachabilityStatus very fast (regardless of connection) when I (a) set availability at startup; but (b) check for connectivity in just-in-time mode.

And if you absolutely need to start processing immediately, then the question arises whether you can direct this to the background (for example, dispatch_async() ). For example, my application receives updates from the server, but since this happens in the background, neither I nor my user are aware of the delays.

+3
source

I had problems with the same, but I found a way to specify a timeout. I replaced this method inside Apple's Reachability class.

 - (NetworkStatus)currentReachabilityStatus { NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef"); //NetworkStatus returnValue = NotReachable; __block SCNetworkReachabilityFlags flags; __block BOOL timeOut = NO; double delayInSeconds = 5.0; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(delay, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){ timeOut = YES; }); __block NetworkStatus returnValue = NotReachable; __block BOOL returned = NO; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) { if (_alwaysReturnLocalWiFiStatus) { returnValue = [self localWiFiStatusForFlags:flags]; } else { returnValue = [self networkStatusForFlags:flags]; } } returned = YES; }); while (!returned && !timeOut) { if (!timeOut && !returned){ [NSThread sleepForTimeInterval:.02]; } else { break; } } return returnValue; } 
0
source

Source: https://habr.com/ru/post/912929/


All Articles