IOS Reachability - Problems with Slow and Missing Notifications

I use the sample code provided by Apple to track reachability, but it still causes me a headache. I work on iOS 5 by the way.

// Initialise hostReach = [[Reachability reachabilityForInternetConnection] retain]; [hostReach startNotifier]; 

and

 -(void) updateConnectionStatus { // Check Internet connectivity NetworkStatus netStatus = [hostReach currentReachabilityStatus]; if(netStatus == NotReachable) { NSLog (@"updateConnectionStatus: network not reachable!"); [self setNetworkAvailable:NO]; } else { NSLog (@"updateConnectionStatus: network reachable!"); [self setNetworkAvailable:YES]; } } 

as well as

 //Called by Reachability whenever status changes. - (void) reachabilityChanged: (NSNotification *) note { NSLog (@"reachabilityChanged"); [self updateConnectionStatus]; } 

not forgetting

 - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog (@"applicationWillEnterForeground"); [self updateConnectionStatus]; } 

I used this for a while, but I want to handle accessibility in a non-trivial way in the application, for example, using cached data when the network is unavailable.

The first problem is that notification of a status change does not arrive until around the 10th day after the application has entered the foreground, and even manually checking when the application returns to the foreground (as shown above) returns an incorrect result. See this sample log from the console,

 2012-02-01 13:31:02.566 myapp[9807:707] applicationWillEnterForeground 2012-02-01 13:31:02.632 myapp[9807:707] Reachability Flag Status: -- ------- networkStatusForFlags 2012-02-01 13:31:02.634 myapp[9807:707] updateConnectionStatus: network not reachable! 2012-02-01 13:31:02.660 myapp[9807:707] reachabilityChanged 2012-02-01 13:31:02.662 myapp[9807:707] Reachability Flag Status: WR t----l- networkStatusForFlags 2012-02-01 13:31:02.663 myapp[9807:707] updateConnectionStatus: network reachable! 

I tried both

 hostReach = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain]; 

and

 hostReach = [[Reachability reachabilityForInternetConnection] retain]; 

This means that if the network was unavailable the last time the application was launched, recovery is less effective than when the application starts again with a network connection.

Is this delay inevitable?

My other problem is that my application does not seem to receive these notifications sometimes. This happens when my application is in the background for some time, although I cannot recreate it on my own.

But sometimes, when I return to the application, the last notification it received was that the network was inaccessible, and it apparently never sees the notification of network achievement: - (

Has anyone else seen something like this?

Thanks.

+4
source share
1 answer

To answer my own question, calling updateConnectionStatus in a separate thread seems to fix the problem (it seems to return the correct status more reliably), although to be honest, I'm not sure why.

Delayed notifications remain a nuisance.

0
source

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


All Articles