I'm using the Reachability
class provided here by Apple in my iOS project. I call its currentReachabilityStatus
method always before trying to call my own REST web services:
- (NetworkStatus)currentReachabilityStatus
{
NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
NetworkStatus returnValue = NotReachable;
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
{
if (_alwaysReturnLocalWiFiStatus)
{
returnValue = [self localWiFiStatusForFlags:flags];
}
else
{
returnValue = [self networkStatusForFlags:flags];
}
}
return returnValue;
}
In turn, I call this method from a custom class I made for convenience:
+ (BOOL)checkNetStatus
{
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus status = [reach currentReachabilityStatus];
return [self boolFromStatus:status];
}
I'm performing some tests in an iPhone, enabling the flight mode in its Settings, and then when the app is back to foreground, that method is called several times (my app retries to call the web services if no reachability until they become reachable) and finally I get an EXC_BAD_ACCESS
exception at line if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
, and the app crashes.
I don't understand exactly why, because currentReachabilityStatus
is called several times before I get the exception and the crash, could it be because it is being called a lot of times and too fast? How could I solve this?
I need help, thanks in advance.
EDIT: whenever I'm going to call of my RESTful services, I do something like this:
- (void)callWebService
{
if ([MyReachabilityManager checkNetStatus]) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
MyServiceWrapper *requestService = [[MyServiceWrapper alloc] initWithServiceUrl];
[requestService queryService];
}
else {
[self keepRequestingMyService]; // this calls this method again until a timeout
}
}