2

When there is no connection I get an error from the URL Session saying that the request timed out.
I’m using the Network protocol to check for connectivity before hand but apparently this is not working as when I am calling this inside viewDidLoad:

static func startUpdateProcess() {
    let monitor = NWPathMonitor()

    monitor.pathUpdateHandler = { path in
        if path.status == .satisfied {
            print("Good! We are connected!")
            Helper.createDownloadTask()
        } else {
            print("No connection. Local file not updated!")
        }
    }
    let queue = DispatchQueue(label: "Monitor")
    monitor.start(queue: queue)
}

...I get “Good! We are connected!”. Shouldn’t the path not be satisfied if there is no connection and therefore trigger the else statement?
FYI the createDownloadTask() questions the API and downloads the required data.

Can you tell me what is wrong here and what could I do to get to the else statement if the path is not satisfied?

Thank you!

NotationMaster
  • 390
  • 3
  • 17

2 Answers2

1

Credit to user May Rest in Peace for pointing me to the right direction.

Despite the Documentation being silent on the Network Protocol, it seems that the status property of the NWPath class, an enumeration of type NWPath.Status, returns .satisfied as long as the device is connected to a network, regardless of whether that network is working, transmitting data, or not.

The only way the else statement above could be triggered would have been by deactivating Wi-Fi and/or Cellular data or disconnecting from any network before launching the app.

All those properties are listed in the Documentation but none of them has a description or a discussion attached. This article by user @twostraws allowed me to create the first part of that code.

NotationMaster
  • 390
  • 3
  • 17
0

Reference to the instance of NWPathMonitor (aka monitor in your scenario) needs to be retained.

You could make it a strong property by making monitor a class level property so that its lifecycle is the same as the place you are referring it from. It looks like the monitor object is being released effectively stopping the callbacks for network status monitoring.

May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34
  • Originally it was a class level property but as this method is not in the ViewController.swift file (and class) I was getting an error if that was left outside. I wonder what is making the pathUpdateHandler think that the status is satisfied. I was connected to the network but there was no connection (i.e. the provider was having an issue and there was no data exchange). Does it still count as a successful connection? Should I check for something else? – NotationMaster Aug 14 '19 at 23:32
  • NetworkPathMonitor is supposed to just tell whether you are connected to Internet or not, as much as I know. Try switching off your wifi and cellular data to see if No connection is called – May Rest in Peace Aug 14 '19 at 23:35
  • That was it! I wonder if there is another thing that checks for the connectivity actually exchanging data or not. Thank you anyway! – NotationMaster Aug 15 '19 at 05:41
  • Btw, setting the property to class level with `static` in front also worked, just it looked more logical to me to leave it inside the method definition as this class is only there for providing functionality. – NotationMaster Aug 15 '19 at 07:18
  • Are you using `URLSession` for your download task ? You can get total data downloaded in it and if you run a timer you can also get the speed. [Link](https://stackoverflow.com/questions/33887748/right-way-of-determining-internet-speed-in-ios-8). You can configure for your purposes like if no data transfer happens within a certain amount of time, you can consider it to be failure. – May Rest in Peace Aug 15 '19 at 11:43
  • Yes of course! Thank you for the link. It seems a bit overkill for the scope of my app but, why not? :) Now, should I answer my own question with explanation of how `path` works or just close it? – NotationMaster Aug 15 '19 at 11:53
  • 1
    I think you should answer it. It would be useful for people who stumble upon it. – May Rest in Peace Aug 15 '19 at 12:37