I have a simple class named NetworkPathMonitor that looks as follows:
class NetworkPathMonitor: ObservableObject, Cancellable {
@Published var path: NWPath? = nil
let monitor = NWPathMonitor()
init() {
monitor.pathUpdateHandler = { [weak self] path in
DispatchQueue.main.async {
self?.path = path
}
}
monitor.start(queue: DispatchQueue.global(qos: .background))
}
func cancel() {
monitor.cancel()
}
deinit {
cancel()
}
}
Although pathUpdateHandler
is called when I disable and enable WiFi the outcome is not always reliable. When I disable the WiFi the current path is unsatisfied (No network route)
, however when I enable WiFi again it calls pathUpdateHandler
again with the same path unsatisfied (No network route)
.
However in my UI I have a button that says Retry. When I press it, it will destroy the current monitor and build a new one. The first message now says satisfied (Path is satisfied), interface: en0
. Meaning, perhaps it took some time for the WiFi to establish the network route but when the network did become available it did not call pathUpdateHandler
again.