草庐IT

ios - `SCNetworkReachabilityScheduleWithRunLoop` 还是 `SCNetworkReachabilitySetDispatchQueue`?和比赛条件

coder 2024-01-23 原文

我们的 Objective-C 应用需要检测网络可达性变化。 CocoaPods 目前提供了太多的选择,所以我们不知道如何选择一个。

所有可达性 pod 都使用 SCNetworkReachabilityScheduleWithRunLoopSCNetworkReachabilitySetDispatchQueue:两者之间是否存在行为差异? (除了第一个对 iOS 2 和 iOS 3 的微不足道的支持)

Last comment from Oleg on Mike Ash blog reads :

What bothers me is the potential race condition that can occur when I call SCNetworkReachabilityGetFlags in a helper thread, while the SCNetworkReachabilityScheduleWithRunLoop has already been set in the main run loop.

There are going to be two different threads both checking for network reachability at the same time and returning their result to the main thread. If network status changes somewhere in between, the events may appear in the run loop queue in an incorrect order.

Is there a way to guarantee the correct event order? Ideally, to make the SCNetworkReachabilityScheduleWithRunLoop to trigger events upon request (not only upon actual network status change)

当我们想找出哪些 pod 最可靠时,SCNetworkReachabilitySetDispatchQueue 是否解决了 Oleg 的竞争条件问题?或者在选择 pod 时是否有解决竞争条件问题的模式?

举几个例子,这是我们注意到的......

他们使用SCNetworkReachabilityScheduleWithRunLoop:

他们使用SCNetworkReachabilitySetDispatchQueue:

最佳答案

SCNetworkReachabilityScheduleWithRunLoopSCNetworkReachabilitySetDispatchQueue 应该是等价的。至于竞争条件问题,解决它的明显方法是仅从可达性回调中调用 SCNetworkReachabilityGetFlags。另一个,如果你真的想从并发线程调用它,可能会为它使用一个单独的 SCNetworkReachabilityRef(到同一主机)。

关于ios - `SCNetworkReachabilityScheduleWithRunLoop` 还是 `SCNetworkReachabilitySetDispatchQueue`?和比赛条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433445/

有关ios - `SCNetworkReachabilityScheduleWithRunLoop` 还是 `SCNetworkReachabilitySetDispatchQueue`?和比赛条件的更多相关文章

随机推荐