Issues with NWPathMonitor

With SCNetworkReachabilityCreateWithAddress now depreciated I'm having to use NWPathMonitor to determine if I have a good connection to access some HTML and JSON files. I have this working just fine if the network connection is down but if the network connection comes back up it detects an event but still shows the status as unsatisfied. I can't seem to capture or pickup that when I've got a good connection again so I can proceed with the right status. Here is my current code. Any suggestions would be appreciated:

import Network
import Combine

class NetworkStatus: ObservableObject {
    static let shared = NetworkStatus()

    private var monitor: NWPathMonitor?
    private var queue = DispatchQueue(label: "NetworkMonitor")

    @Published var isConnected: Bool = false

    private init() {
        monitor = NWPathMonitor()
        startMonitoring()
    }

    deinit {
        stopMonitoring()
    }
    
    func startMonitoring() {
        monitor?.pathUpdateHandler = { [weak self] path in
            DispatchQueue.main.async {
                let status = path.status == .satisfied
                self?.isConnected = status
                print(">>> \(path.status)")
            }
        }

        monitor?.start(queue: queue)
    }

    func stopMonitoring() {
        monitor?.cancel()
        monitor = nil
    }
}

I'm having to use NWPathMonitor to determine if I have a good connection to access some HTML and JSON files.

If you’re fetching these over HTTP then we generally recommend that you avoid preflight and instead use URLSession configured with the waitsForConnectivity property.

We recently published a techtalk, Adapt to changing network conditions, that explains the background to this recommendation.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Issues with NWPathMonitor
 
 
Q