IOS dispatch_get_main_queue()被多次调用 [英] IOS dispatch_get_main_queue() is called multiple times

查看:451
本文介绍了IOS dispatch_get_main_queue()被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Reachabilitydispatch_async(dispatch_get_main_queue()测试Internet连接 当我测试以下代码时,它可以工作,但是被多次调用.

父母:

@protocol RootViewDelegate <NSObject>
@optional
-(void)internetIsDownGoToRoot;
@end
- (void)testInternetConnection
{
    internetReachableFoo = [ReachabilityTony reachabilityWithHostname:@"www.google.com"];

    __weak typeof(self) weakSelf = self;
    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(ReachabilityTony *reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Yayyy, we have the interwebs!");
            [weakSelf sendLoginRequest];
        });
    };
        // Internet is not reachable
internetReachableFoo.unreachableBlock = ^(ReachabilityTony *reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Someone broke the internet :(");
        CloudConnection *sharedInstance=[CloudConnection  sharedInstance];
        sharedInstance.isUserLoggedIN=NO;
        //update login button
        [weakSelf updateButtons];
        [weakSelf notifyChild];

    });
};
    [internetReachableFoo startNotifier];
}
-(void)viewDidAppear:(BOOL)animated
{
[self testInternetConnection];
 }
-(void)viewWillDisappear:(BOOL)animated
{
    internetReachableFoo= nil;

}
//notify childs no connection come back to root
-(void) notifyChild
{
    [delegate internetIsDownGoToRoot];

}

孩子:

-(void)viewDidAppear:(BOOL)animated
{

    NSArray *viewControllers =     self.navigationController.viewControllers;
    int count = [viewControllers count];
    id previousController = [viewControllers objectAtIndex:count - 2];
    RootViewController *rvc= previousController;
    rvc.delegate=self;


}

-(void)internetIsDownGoToRoot
{
    //internet connection is no avaliable go to root
    [self.navigationController popToRootViewControllerAnimated:YES];

}

因此,这是parentview,可以说我推送了childview 5次并关闭了互联网.我在nslog上看到

Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(

如您所见,我已经添加了internetReachableFoo= nil;,但是我什么都没做.

上面的代码是怎么回事,为什么多次调用它?

使用此块可能有什么危险?

解决方案

它被多次调用是因为每次您弹出孩子时,根目录都会得到-viewDidAppear:并调用-testInternetConnection,这将重新运行可达性测试. /p>

更新:好的,您已经稍微更改了您的问题.之所以收到5条消失"消息,是因为您从未停止过通知者.可达性只要在运行中就可以保持活跃,因此,完善您的参考文献不会杀死它.您需要在清除之前先明确说出[internetReachableFoo stopNotifier].

I test internet connection with Reachability and dispatch_async(dispatch_get_main_queue() when I test the following code it works but it is called multiple times.

Parent:

@protocol RootViewDelegate <NSObject>
@optional
-(void)internetIsDownGoToRoot;
@end
- (void)testInternetConnection
{
    internetReachableFoo = [ReachabilityTony reachabilityWithHostname:@"www.google.com"];

    __weak typeof(self) weakSelf = self;
    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(ReachabilityTony *reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Yayyy, we have the interwebs!");
            [weakSelf sendLoginRequest];
        });
    };
        // Internet is not reachable
internetReachableFoo.unreachableBlock = ^(ReachabilityTony *reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Someone broke the internet :(");
        CloudConnection *sharedInstance=[CloudConnection  sharedInstance];
        sharedInstance.isUserLoggedIN=NO;
        //update login button
        [weakSelf updateButtons];
        [weakSelf notifyChild];

    });
};
    [internetReachableFoo startNotifier];
}
-(void)viewDidAppear:(BOOL)animated
{
[self testInternetConnection];
 }
-(void)viewWillDisappear:(BOOL)animated
{
    internetReachableFoo= nil;

}
//notify childs no connection come back to root
-(void) notifyChild
{
    [delegate internetIsDownGoToRoot];

}

Child:

-(void)viewDidAppear:(BOOL)animated
{

    NSArray *viewControllers =     self.navigationController.viewControllers;
    int count = [viewControllers count];
    id previousController = [viewControllers objectAtIndex:count - 2];
    RootViewController *rvc= previousController;
    rvc.delegate=self;


}

-(void)internetIsDownGoToRoot
{
    //internet connection is no avaliable go to root
    [self.navigationController popToRootViewControllerAnimated:YES];

}

So this is parentview lets say I push-pop childview 5 times and shutdown internet. I see on nslog that

Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(

as you can see I have added internetReachableFoo= nil; but I doesnt change anything.

Whats going on with above code, why it is called multiple times?

What is the possible dangers of using this block?

解决方案

It's called multiple times because every time you pop the child, the root gets -viewDidAppear: and calls -testInternetConnection, which re-runs the reachability test.

Update: Ok you've changed your question slightly. The reason why you're getting 5 "did disappear" messages is because you never stop the notifier. Reachability keeps itself alive as long as it's running, so nilling out your reference isn't killing it. You need to explicitly say [internetReachableFoo stopNotifier] before nilling it out.

这篇关于IOS dispatch_get_main_queue()被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆