From 6941a8b857861895cf38140b4cf3e51e7952422b Mon Sep 17 00:00:00 2001 From: Murat Baysangurov Date: Fri, 8 Dec 2017 16:21:49 +0300 Subject: [PATCH 1/3] Sync lazy creation in props --- Classes/Telemetry/BITMetricsManager.m | 32 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Classes/Telemetry/BITMetricsManager.m b/Classes/Telemetry/BITMetricsManager.m index 0d34b709..07265322 100644 --- a/Classes/Telemetry/BITMetricsManager.m +++ b/Classes/Telemetry/BITMetricsManager.m @@ -233,31 +233,39 @@ - (void)trackDataItem:(BITTelemetryData *)dataItem { #pragma mark - Custom getter - (BITChannel *)channel { - if (!_channel) { - _channel = [[BITChannel alloc] initWithTelemetryContext:self.telemetryContext persistence:self.persistence]; + @synchronized(self) { + if (!_channel) { + _channel = [[BITChannel alloc] initWithTelemetryContext:self.telemetryContext persistence:self.persistence]; + } + return _channel; } - return _channel; } - (BITTelemetryContext *)telemetryContext { - if (!_telemetryContext) { - _telemetryContext = [[BITTelemetryContext alloc] initWithAppIdentifier:self.appIdentifier persistence:self.persistence]; + @synchronized(self) { + if (!_telemetryContext) { + _telemetryContext = [[BITTelemetryContext alloc] initWithAppIdentifier:self.appIdentifier persistence:self.persistence]; + } + return _telemetryContext; } - return _telemetryContext; } - (BITPersistence *)persistence { - if (!_persistence) { - _persistence = [BITPersistence new]; + @synchronized(self) { + if (!_persistence) { + _persistence = [BITPersistence new]; + } + return _persistence; } - return _persistence; } - (NSUserDefaults *)userDefaults { - if (!_userDefaults) { - _userDefaults = [NSUserDefaults standardUserDefaults]; + @synchronized(self) { + if (!_userDefaults) { + _userDefaults = [NSUserDefaults standardUserDefaults]; + } + return _userDefaults; } - return _userDefaults; } @end From 8154cf2a90801288f500e21273325c8c9aa510c4 Mon Sep 17 00:00:00 2001 From: Murat Baysangurov Date: Fri, 8 Dec 2017 16:22:11 +0300 Subject: [PATCH 2/3] Sync channel timer operations --- Classes/Telemetry/BITChannel.m | 50 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/Classes/Telemetry/BITChannel.m b/Classes/Telemetry/BITChannel.m index edfc5572..c122cb18 100644 --- a/Classes/Telemetry/BITChannel.m +++ b/Classes/Telemetry/BITChannel.m @@ -214,37 +214,43 @@ - (NSUInteger)maxBatchSize { } - (void)invalidateTimer { - if ([self timerIsRunning]) { - dispatch_source_cancel((dispatch_source_t)self.timerSource); - self.timerSource = nil; + @synchronized(self) { + if (self.timerSource != nil) { + dispatch_source_cancel((dispatch_source_t)self.timerSource); + self.timerSource = nil; + } } } -(BOOL)timerIsRunning { - return self.timerSource != nil; + @synchronized(self) { + return self.timerSource != nil; + } } - (void)startTimer { - // Reset timer, if it is already running - if ([self timerIsRunning]) { + @synchronized(self) { + + // Reset timer, if it is already running. [self invalidateTimer]; - } - - self.timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.dataItemsOperations); - dispatch_source_set_timer((dispatch_source_t)self.timerSource, dispatch_walltime(NULL, NSEC_PER_SEC * self.batchInterval), 1ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC); - __weak typeof(self) weakSelf = self; - dispatch_source_set_event_handler((dispatch_source_t)self.timerSource, ^{ - typeof(self) strongSelf = weakSelf; - if (strongSelf) { - if (strongSelf.dataItemCount > 0) { - [strongSelf persistDataItemQueue]; - } else { - strongSelf.channelBlocked = NO; + + dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.dataItemsOperations); + dispatch_source_set_timer(timerSource, dispatch_walltime(NULL, NSEC_PER_SEC * self.batchInterval), 1ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC); + __weak typeof(self) weakSelf = self; + dispatch_source_set_event_handler(timerSource, ^{ + typeof(self) strongSelf = weakSelf; + if (strongSelf) { + if (strongSelf.dataItemCount > 0) { + [strongSelf persistDataItemQueue]; + } else { + strongSelf.channelBlocked = NO; + } + [strongSelf invalidateTimer]; } - [strongSelf invalidateTimer]; - } - }); - dispatch_resume((dispatch_source_t)self.timerSource); + }); + dispatch_resume(timerSource); + self.timerSource = timerSource; + } } /** From b4a47d623d567610e4500e7e3828f5b8004d1a47 Mon Sep 17 00:00:00 2001 From: Murat Baysangurov Date: Fri, 8 Dec 2017 16:22:47 +0300 Subject: [PATCH 3/3] Create context dictionary on operations queue --- Classes/Telemetry/BITTelemetryContext.m | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Classes/Telemetry/BITTelemetryContext.m b/Classes/Telemetry/BITTelemetryContext.m index e043ffed..655acd90 100644 --- a/Classes/Telemetry/BITTelemetryContext.m +++ b/Classes/Telemetry/BITTelemetryContext.m @@ -352,12 +352,13 @@ - (void)setDeviceType:(NSString *)deviceType { #pragma mark - Helper - (NSDictionary *)contextDictionary { - NSMutableDictionary *contextDictionary = [NSMutableDictionary new]; - [contextDictionary addEntriesFromDictionary:self.tags]; - [contextDictionary addEntriesFromDictionary:[self.session serializeToDictionary]]; - [contextDictionary addEntriesFromDictionary:[self.user serializeToDictionary]]; - - return contextDictionary; + __block NSMutableDictionary *tmp = [NSMutableDictionary new]; + dispatch_sync(self.operationsQueue, ^{ + [tmp addEntriesFromDictionary:self.tags]; + [tmp addEntriesFromDictionary:[self.session serializeToDictionary]]; + [tmp addEntriesFromDictionary:[self.user serializeToDictionary]]; + }); + return tmp; } - (NSDictionary *)tags {