nw_path_monitor_t / NWPathMonitor: is cancelling on background / resuming on foreground necessary?

225 Views Asked by At

I'm using the Network.framework package on iOS to monitor for network changes. I'm using Objective C, but same underlying code as Swift NWPathMonitor. Everything is working great with code below, but curious if there's any long term lifecycle considerations I'm missing.

Should I be canceling the path monitor when the app enters the background, and restarting when it enters foreground? Or is it okay to leave it in background for an indefinite periods, and it will not consume additional resources, and it will resume fine upon re-foregrounding?

I'm not expecting updates when in the background, I just want to make sure it's:

  1. Not consuming significant additional resources, or will increase chance of app being removed from memory
  2. It will get an update when the app resumes in foreground, catching it up to any updates it might have missed in background.
        self.monitor = nw_path_monitor_create();
        nw_path_monitor_set_queue(
            self.monitor,
            dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
        __weak CMNetworkMonitor *weakSelf = self;
        nw_path_monitor_set_update_handler(
            self.monitor, ^(nw_path_t _Nonnull path) {
              weakSelf.currentPath = path;
              dispatch_semaphore_signal(weakSelf.readReadySemaphore);
            });
        nw_path_monitor_start(self.monitor);
0

There are 0 best solutions below