2

I make use of Apples Reachability class and it works if I keep it on the main thread(bad approach). If I instead move it to a separate thread the notification is never called.

In didFinishLaunchingWithOptions i call the following:

[NSThread detachNewThreadSelector:@selector(checkConnection) toTarget:self withObject: nil];

checkConnection looks like follows:

-(void)checkConnection {
//Test for Internet Connection
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
Reachability *r = [[Reachability reachabilityWithHostName:@"appspot.com"] retain];
[r updateReachability:appDelegate.reachability];
[r startNotifier];
[pool release]; 
}

and reachabilityChanged looks like this:

- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateReachability: curReach];
}

and finally updateReachability looks like this:

- (void)updateReachability:(Reachability *)curReach {
NetworkStatus internetStatus = [curReach currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
    NSLog(@"No net");
} else {
    NSLog(@"Lots of net");
}}

Hope you guys can help me to understand why reachabilityChanged is never called.

Cheers...

mbogh
  • 1,361
  • 11
  • 28

2 Answers2

1

I've used Apple's reachability example for the Mac and have had it working fine. You are suppose to run reachability on the main thread. The whole point of the reachability is that you don't have to pool for an internet connection. The system automatically spawns a background thread to monitor for changes in interent connection and will notify you of any changes.

Have your app ever stalled because of reachability or are you just postulating that it might happen?

David
  • 14,205
  • 20
  • 97
  • 144
0

See this answer:

Reachability sometimes fails, even when we do have an internet connection

Community
  • 1
  • 1
sehugg
  • 3,615
  • 5
  • 43
  • 60