I'm using the NWPathMonitor class to check the internet changes. But I have an issue when using it. The issue is when internet is off and once it comes back, monitor.pathUpdateHandler is getting called/notifying twice. Where as in internet off case it is getting called only once. So, what is the mistake I'm doing here?
Below is my code snippet
final class NetworkMonitor {
static let shared = NetworkMonitor()
private let queue = DispatchQueue.global(qos: .background)
private let monitor: NWPathMonitor
private(set) var isConnected = false
private(set) var isExpensive = false
private(set) var currentConnectionType: NWInterface.InterfaceType?
private init() {
monitor = NWPathMonitor()
}
func startMonitoring() {
monitor.pathUpdateHandler = { [weak self] path in
self?.isExpensive = path.isExpensive
self?.currentConnectionType = NWInterface.InterfaceType.allCases.filter { path.usesInterfaceType($0) }.first
NotificationCenter.default.post(name: .connectivityStatus, object: nil)
}
monitor.start(queue: queue)
}
func stopMonitoring() {
monitor.cancel()
}
}
If you save NWPath and compare them.
You will found that paths are different.
Ex:
First is
.cellular
.Second is
.cellular
+.wifi
.Update for iOS 16.6
pathUpdateHandler
will fired three times. Ex:First is
.cellular
+.wifi
.Second is
.wifi
+.cellular
. third is.wifi
+.cellular
.