Iphone 独立线程上通知的可达性问题
我使用了苹果的可达性类,如果我把它保持在主线程上,它就会工作(糟糕的方法)。如果我将其移动到一个单独的线程,则不会调用通知 在Iphone 独立线程上通知的可达性问题,iphone,nsthread,reachability,Iphone,Nsthread,Reachability,我使用了苹果的可达性类,如果我把它保持在主线程上,它就会工作(糟糕的方法)。如果我将其移动到一个单独的线程,则不会调用通知 在didfishlaunchingwithoptions中,我调用以下命令: [NSThread detachNewThreadSelector:@selector(checkConnection) toTarget:self withObject: nil]; checkConnection如下所示: -(void)checkConnection { //Test fo
didfishlaunchingwithoptions
中,我调用以下命令:
[NSThread detachNewThreadSelector:@selector(checkConnection) toTarget:self withObject: nil];
checkConnection如下所示:
-(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];
}
可达性更改如下所示:
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateReachability: curReach];
}
- (void)updateReachability:(Reachability *)curReach {
NetworkStatus internetStatus = [curReach currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
NSLog(@"No net");
} else {
NSLog(@"Lots of net");
}}
最后,可更新性如下所示:
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateReachability: curReach];
}
- (void)updateReachability:(Reachability *)curReach {
NetworkStatus internetStatus = [curReach currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
NSLog(@"No net");
} else {
NSLog(@"Lots of net");
}}
希望你们能帮助我理解为什么从未调用reachabilityChanged
干杯…请看以下答案:
我在Mac上使用了苹果的可达性示例,并使其运行良好。假设您在主线程上运行可达性。可访问性的全部要点是,您不必为internet连接共用资源。系统会自动生成一个后台线程来监视interent连接中的更改,并将通知您任何更改
你的应用程序是否曾经因为可访问性而停止运行,或者你只是假设它可能会发生?为什么你认为将其保留在主线程上是一种不好的方法?DNS问题会使应用程序暂停几分钟。