如何在iOS上定义可达性超时 [英] How to define a reachability timeout on ios

查看:76
本文介绍了如何在iOS上定义可达性超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用可达性类来确定我是否有可用的互联网连接.问题是当wifi可用但没有Internet时,-(NetworkStatus)currentReachabilityStatus 方法会花费太多时间.

I use the Reachability class to know if I have an internet connection available. The problem is when wifi is available but not internet, the - (NetworkStatus) currentReachabilityStatus method take too much time.

我的代码:

Reachability* reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

应用程序暂时在第二行冻结".如何定义此等待的最长时间?

The application "freeze" temporarily on the second line. How to define the maximum time for this waiting ?

推荐答案

我不这么认为.但更重要的是,我认为您不愿意(您可能会得到误报).让可达性"顺其自然.

I don't think so. But more importantly, I don't think you'd want to if you could (you may get false positives). Let Reachability run it's course.

如果您查看Reachability演示项目,则该概念不是在需要Internet时调用 reachabilityWithHostName 并检查 currentReachabilityStatus .您在应用程序委托的didFinishLaunchingWithOptions期间调用currentReachabilityStatus,设置通知,当Internet连接发生更改时,Reachability会告诉.我发现,当我(a)在启动时设置可达性时,对 currentReachabilityStatus 的后续检查非常快速(与连接无关).但是(b)及时检查连通性.

If you look at the Reachability demo project, the notion isn't to invoke reachabilityWithHostName and check currentReachabilityStatus when you need the Internet. You invoke currentReachabilityStatus at during your app delegate's didFinishLaunchingWithOptions, set up a notification, and Reachability will tell you when the Internet connectivity has changed. I find that subsequent checks to currentReachabilityStatus are plenty fast (regardless of connectivity) when I (a) setup reachability at startup; but (b) check for connectivity in a just-in-time manner.

如果您绝对需要立即开始处理,那么问题是您是否可以将其推送到后台(例如 dispatch_async()).例如,我的应用程序从服务器检索更新,但是由于更新是在后台进行的,因此我和我的用户都没有意识到任何延迟.

And if you absolutely need to start your processing immediately, then the question is whether you can push that into the background (e.g. dispatch_async()). E.g., my app retrieves updates from the server, but because that's happening in the background, neither me nor my user are aware of any delays.

这篇关于如何在iOS上定义可达性超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆