I have been using the SCNetworkReachabilityGetFlags for 10+ years to inform users that their request won't work. In my experience this works pretty well although i am aware of the limitations.
Now, i am looking into the NWPathMonitor, and i have one situation that i'm trying to. get my head around - it's asynchronous.
Specifically, i am wondering what to do when my geofences trigger and i want to check network connectivity - i want to tell the user why the operation i'll perform because of the trigger couldn't be done.
SO. say i start a NWPathMonitor in didFinishLaunchingWithOptions. When the app is booted up because of a geofence trigger, might i not end up in a case where my didEnterRegion / didExitRegion gets called before the NWPathMonitor has gotten its first status?
The advantage here with SCNetworkReachabilityGetFlags, as i understand it, would be that it's synchronous?
If i want to upgrade to nwpathmonitor, i guess i have to do a method that creates a nwpathmonitor, uses a semaphore to wait for the first callback, then contunues?
Thoughts appreciated
just fire off the request blindly and wait to see what happens?
Yes. And use an expiration handler to deal with the case where the network is super slow.
That last bit is critical, because it crops up in practice. In most cases an immediate network request — that is, one without waitsForConnectivity set — will finish promptly. It’ll either succeed or it’ll fail immediately. But there are networks out there in the real world where such requests take a long time. Sometimes that’s caused by bad network conditions. The network is moving packets just fast enough to convince iOS to use it, but not fast enough to get anything useful done. But in other cases it’s caused by weird network configurations. For example, a Wi-Fi accessory might publish a network that goes out of its way to convince iOS that it has a path to the wider Internet when it actually doesn’t.
Now, unless you have access to such a network then testing your expiration handler can be tricky. My advice is that you add a mode to your networking layer to specifically simulate this. That is, you pass it a request and in this mode the network layer simply never completes the request.
Oh, and make sure to add logging so that you can investigate problems that shows up in the field. I have advice about this in Testing Background Session Code (you’re not using a background session but a lot of the advice still applies), and general advice about logging in Your Friend the System Log.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"