Mord Fustang
Mord Fustang

Reputation: 1553

IOS dispatch_get_main_queue() is called multiple times

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?

Upvotes: 0

Views: 1192

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185653

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.

Upvotes: 4

Related Questions