2011-04-10 60 views
0

我使用下面的代码来测试网络连接是否可用,如何更快地实现可达性?

NSLog(@"Testing internet connection"); 
Reachability *internetReach = [Reachability reachabilityWithHostName:@"google.com"]; 
if([internetReach currentReachabilityStatus] == NotReachable) { 
    NSLog(@"Internet connection is not Rechable"); 
} else { 
    NSLog(@"Internet connection is Rechable"); 
} 

但它占用太多时间来检验了互联网连接,是有什么办法可以减少时间?

由于

回答

3

不幸的代码本身就要求访问网络,这可能需要的时间量的无界(考虑极其缓慢或不可靠的3G热点,例如)。

如果可能的话,您应该将该工作移到背景队列中以避免阻塞主线程。