Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Fix crashes by concurrency #134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions Classes/Telemetry/BITChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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