views:

36

answers:

0

Hello,

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...