Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure network monitor path update handler is called on start #199

Merged
20 changes: 17 additions & 3 deletions Sources/Event Logging/TracksService.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ @interface TracksService ()
@property (nonatomic, strong) TracksContextManager *contextManager;
@property (nonatomic, strong) TracksDeviceInformation *deviceInformation;
@property (nonatomic, strong) nw_path_monitor_t networkMonitor;
/// The queue on which the Network framework will dispatch all the executions of the update and
/// cancel events handlers.
@property (nonatomic, strong) dispatch_queue_t networkMonitorQueue;
@property (nonatomic, strong) nw_path_t networkPath;

@property (nonatomic, readonly) NSString *userAgent;
Expand Down Expand Up @@ -317,12 +320,23 @@ - (void)startNetworkMonitor {
return;
}

__weak typeof(self) weakSelf = self;
self.networkMonitor = nw_path_monitor_create();
nw_path_monitor_set_update_handler(_networkMonitor, ^(nw_path_t _Nonnull path) {

// Using `dispatch_queue_create_with_target` to create a serial queue that will target one of
// the existing concurrent views seems to be the approach Apple recommends.
//
// More at: https://github.com/Automattic/Automattic-Tracks-iOS/pull/199#discussion_r822683662 .
dispatch_queue_t utilityQueue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
self.networkMonitorQueue = dispatch_queue_create_with_target("com.automattic.tracks.network.monitor",
DISPATCH_QUEUE_SERIAL,
utilityQueue);
nw_path_monitor_set_queue(self.networkMonitor, self.networkMonitorQueue);

__weak typeof(self) weakSelf = self;
nw_path_monitor_set_update_handler(self.networkMonitor, ^(nw_path_t _Nonnull path) {
[weakSelf networkPathChanged:path];
});
nw_path_monitor_start(_networkMonitor);
nw_path_monitor_start(self.networkMonitor);
}

- (void)stopNetworkMonitor {
Expand Down