2

Our Objective-C app requires detection of network reachability changes. CocoaPods offers currently way too many alternatives, so we don't know how to select one.

All reachability pods uses either SCNetworkReachabilityScheduleWithRunLoop or SCNetworkReachabilitySetDispatchQueue: are there behaviour differences to expect from one or the other? (apart from trivial support of iOS 2 and iOS 3 for the first one)

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)

As we'd like to figure out which pods are the most reliable, is SCNetworkReachabilitySetDispatchQueue addressing a solution to Oleg's race condition issue? Or is there a pattern to solve race condition issues that we should look for when choosing a pod?

To list a few examples, here is what we noticed...

They use SCNetworkReachabilityScheduleWithRunLoop:

They use SCNetworkReachabilitySetDispatchQueue:

Community
  • 1
  • 1
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Both SCNetworkReachabilityScheduleWithRunLoop and SCNetworkReachabilitySetDispatchQueue should be equivalent. As for the race condition issue, the obvious way to solve it is to only call SCNetworkReachabilityGetFlags from your reachability callback. Another one, if you really want to call it from a concurrent thread, would probably be using a separate SCNetworkReachabilityRef for it (to the same host).

jcanizales
  • 133
  • 1
  • 2
  • 11