NWPathMonitor's pathUpdateHandler block is getting called on app background to foreground transition

853 views Asked by At

Expected NWPathMonitor's pathUpdateHandler will get called only when there is a change in the network activity (e.g., cellular->wifi or wifi1->wifi2 or wifi->no network etc...).

What I am observing is 'pathUpdateHandler' block gets called even when the app in the background for say 30secs and comes to foreground. This isn't expected. Also when network changes 'pathUpdateHandler' block gets invoked multiple times.

Is this expected behaviour? Or is there a workaround for this kind of behaviour? My device is iOS 13.7 Here is my code snippet

-(void) startMonitor {

self.pathMonitor = nw_path_monitor_create();
nw_path_monitor_set_update_handler(self.pathMonitor, ^(nw_path_t  _Nonnull path) {
    
    DDLogVerbose(@"Network path changed %d", nw_path_get_status(path));
    if (nw_path_uses_interface_type(path, nw_interface_type_wifi))  {
        DDLogVerbose(@"Network path user interface type WiFi");
    } else if (nw_path_uses_interface_type(path, nw_interface_type_cellular))  {
        DDLogVerbose(@"Network path user interface type Cellular");
    } else if (nw_path_uses_interface_type(path, nw_interface_type_wired))  {
        DDLogVerbose(@"Network path user interface type Wired");
    } else if (nw_path_uses_interface_type(path, nw_interface_type_loopback))  {
        DDLogVerbose(@"Network path user interface type Loopback");
    } else if (nw_path_uses_interface_type(path, nw_interface_type_other))  {
        DDLogVerbose(@"Network path user interface type Other");
    }
});
nw_path_monitor_start(self.pathMonitor);
}
0

There are 0 answers