Reachability icon indicating copy to clipboard operation
Reachability copied to clipboard

使用blocks 方法监听,当网络状态变化时,blocks会调用两次

Open zhufengyi01 opened this issue 8 years ago • 2 comments

我是使用cocoapod导入的 :pod "Reachability",'~>3.2' 我在didFinishLaunchingWithOptions方法中使用这个 Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    // keep in mind this is called on a background thread
    // and if you are updating the UI it needs to happen
    // on the main thread, like this:

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"网络可用了");
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"网络连接失败了!");
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

当网络状态变化时,会调用两次blocks

zhufengyi01 avatar Mar 08 '16 09:03 zhufengyi01

reach 初始化了两遍, 所以调用了两次 ~

fanzhengfeng avatar Jun 15 '16 07:06 fanzhengfeng

我也遇到了这个问题,但是应该没有初始化两次,很奇怪

jdk89757 avatar Aug 01 '16 08:08 jdk89757