Reachability reachable和unreachableBlock被多次调用?
Reachability reachable and unreachableBlock are called multiple times?
我正在使用 Reachability 检查我的应用程序的网络状态,一切正常,除了在 iOS 9.0.1 或更高版本中 reachableBlock
和 unreachableBlock
被调用两次,这给我惹上大麻烦了。
这只发生在 iOS 9.0.1 和 iOS 9.1 Beta 中。
下面是我的代码示例:
-(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!");
}
}
如果有人解决了这个问题,请告诉我如何解决。
这正是我的想法,这应该是一条评论,但它太长了。
请让我解释一下您的代码中发生了什么...
我不太确定这个,但我认为 iOS 9.0.1 和 iOS 9.1 因为这个 [reach startNotifier];
..
而保留变量
当您调用方法 -(void)checkServerConnection
时,您正在创建一个 new Reachability* reach
等等..
如果您再次调用该方法,它将创建一个 new
Reachability* reach
并且已经存在一个..
可能的解决方案是创建一个全局变量 (Reachability* reach),每次调用该方法时都会重复使用它。
大概是这样的:
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!");
}
}
我正在使用 Reachability 检查我的应用程序的网络状态,一切正常,除了在 iOS 9.0.1 或更高版本中 reachableBlock
和 unreachableBlock
被调用两次,这给我惹上大麻烦了。
这只发生在 iOS 9.0.1 和 iOS 9.1 Beta 中。
下面是我的代码示例:
-(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!");
}
}
如果有人解决了这个问题,请告诉我如何解决。
这正是我的想法,这应该是一条评论,但它太长了。
请让我解释一下您的代码中发生了什么...
我不太确定这个,但我认为 iOS 9.0.1 和 iOS 9.1 因为这个 [reach startNotifier];
..
当您调用方法 -(void)checkServerConnection
时,您正在创建一个 new Reachability* reach
等等..
如果您再次调用该方法,它将创建一个 new
Reachability* reach
并且已经存在一个..
可能的解决方案是创建一个全局变量 (Reachability* reach),每次调用该方法时都会重复使用它。
大概是这样的:
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!");
}
}