gpt4 book ai didi

iphone - 单独线程上的通知的可达性问题

转载 作者:行者123 更新时间:2023-12-03 21:22:37 24 4
gpt4 key购买 nike

我使用Apples Reachability 类,如果我将其保留在主线程上(不好的方法),它就可以工作。如果我将其移至单独的线程,则永远不会调用通知。

didFinishLaunchingWithOptions中我调用以下内容:

[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];
}

reachabilityChanged 看起来像这样:

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

最后 updateReachability 看起来像这样:

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

希望你们能帮助我理解为什么 reachabilityChanged 从未被调用。

干杯...

最佳答案

我在 Mac 上使用了 Apple 的可达性示例,并且运行良好。您应该在主线程上运行可达性。可访问性的全部意义在于,您不必汇集互联网连接。系统自动生成一个后台线程来监视互联网连接的变化,并会通知您任何变化。

您的应用是否曾因可达性而停滞,或者您只是假设这种情况可能会发生?

关于iphone - 单独线程上的通知的可达性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3363084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com