bmueller
bmueller

Reputation: 2711

Where do you put your call to Reachability?

I'm trying to implement Reachability in my app. I have it in applicationDidFinishLaunching. If my connection is really bad and I launch the app, Reachability takes forever and often crashes the app with an The application failed to launch in time error message.

So instead, I tried putting the call to Reachability in a background thread. But when I do this, I no longer receive the Reachability notifications.

Where should I be calling Reachability?

Edit:

I added TonyMillions' Reachability code per the suggestion below, but I'm still getting the same application failed to load in time error when in very bad network conditions. To reproduce what I'm seeing, go to Settings.app -> Developer -> Network Link Conditioner, turn it on and set it to 100% loss.

Does your app still load in that condition?

Upvotes: 5

Views: 2316

Answers (3)

bmueller
bmueller

Reputation: 2711

TonyMillion helped me solve this over on GitHub.

If you're using his Reachability replacement, all you have to do is wrap the startNotifier call like so:

dispatch_async(dispatch_get_global_queue(0,0), ^{
    [self.internetReach startNotifier];
});

The app won't fail to launch in bad network conditions, and you'll still get notifications on the main thread.

Upvotes: 0

Hackmodford
Hackmodford

Reputation: 3970

Use Tony Millions reachability project on Github.

https://github.com/tonymillion/Reachability

Then in my app delegate I just put in applicationdidfinishlaunching

self.reachability = [Reachability reachabilityForInternetConnection];

[self.reachability startNotifier];

Then when the status changes this will be called

#pragma mark - Reachability

- (void)reachabilityChanged:(NSNotification *)note {

    NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];

    if (ns == NotReachable) {

        if (![self.networkAlert isVisible]) {

            if ([self networkAlert] == nil) {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to communicate with the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [self setNetworkAlert:alert];
            }

            [self.networkAlert show];  
        }
    } else {

        if ([self networkAlert] != nil) {

            [self.networkAlert dismissWithClickedButtonIndex:0 animated:YES]; 
        }
    }
}

Upvotes: 2

8suhas
8suhas

Reputation: 1460

So instead, I put the call to Reachability in a background thread. But when I do this, I no longer receive the Reachability notifications.

When you put the call to Reachability in background thread, you should schedule Reachability in background run loop and make sure that the background thread runloop is running.

to be executed in background thread:

  1. create reachbility object,

    Reachability * reachability = [Reachability reachabilityWithHostName:@"www.google.com"];

  2. [reachability startNotifier];

  3. [[NSRunLoop currentRunLoop] run];

This might work but scheduling notifier in main thread is recommended.

Upvotes: 0

Related Questions