Im using Reachability to check the network status for my app, everithing works OK except that in iOS 9.0.1 or higher the reachableBlock
and unreachableBlock
are called twice, which takes me into a big trouble.
This is only happening in iOS 9.0.1 and iOS 9.1 Beta.
And heres an example of my code:
-(void)checkServerConnection{
//This nslog is to check the method is called only once.
NSLog(@"Check Server Connection");
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
[reach startNotifier];
reach.reachableBlock = ^(Reachability*reach)
{
//This NSLOG is called twice
NSLog(@"Reachability reachable block");
dispatch_async(dispatch_get_main_queue(), ^{
//This NSLOG is called twice
NSLog(@"REACHABLE!");
});
};
reach.unreachableBlock = ^(Reachability*reach)
{
//Same story for this one..
NSLog(@"UNREACHABLE!");
}
}
Please if someone have solved this issue let me know how.
This is just what i have in mind, this should be a comment but it's too long.
Please let me explain what is happening in your code...
I'm not really sure about this one but i think iOS 9.0.1 and iOS 9.1 is keeping the variable because of this [reach startNotifier];
..
When you call the method -(void)checkServerConnection
you are creating a new Reachability* reach
and so on..
IF you will call the method again, it will create a new
Reachability* reach
and there is an existing one..
Possible solution is to create a global variable (Reachability* reach) that will be reused everytime the method is called.
Probably something like this:
Reachability* reachVar;
// i've changed your variable name hope that's okay, just for example.
-(void)checkServerConnection{
NSLog(@"Check Server Connection");
if (reachVar == nil)
{
reachVar = [Reachability reachabilityWithHostname:@"www.google.com"];
[reachVar startNotifier];
}
reachVar.reachableBlock = ^(Reachability*reach)
{
NSLog(@"Reachability reachable block");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"REACHABLE!");
});
};
reachVar.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"UNREACHABLE!");
}
}