Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #479 from MatkovIvan/fix/mt-crashes
Browse files Browse the repository at this point in the history
Fix crashes caused by concurrency
  • Loading branch information
Benjamin Scholtysik (Reimold) authored Dec 5, 2017
2 parents 5121bcc + e5a5b9b commit c014376
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
51 changes: 28 additions & 23 deletions Classes/BITChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -426,38 +426,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:&BITTelemetryEventBuffer];
} 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:&BITTelemetryEventBuffer];
} else {
strongSelf.channelBlocked = NO;
}
[strongSelf invalidateTimer];
}
[strongSelf invalidateTimer];
}
});
dispatch_resume((dispatch_source_t)self.timerSource);
});
dispatch_resume(timerSource);
self.timerSource = timerSource;
}
}

/**
Expand Down
32 changes: 20 additions & 12 deletions Classes/BITMetricsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,31 +242,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
Expand Down
13 changes: 7 additions & 6 deletions Classes/BITTelemetryContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,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 {
Expand Down

0 comments on commit c014376

Please sign in to comment.