diff options
author | Julian Rex <julian.rex@mapbox.com> | 2019-07-15 21:11:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-15 21:11:17 -0400 |
commit | 3c488e05efdb9415e658fa820504692b906c3443 (patch) | |
tree | 25f883c677594f212b9c9c5ac6291e8e99f77054 /platform/darwin | |
parent | cdd47c035807583c0437bd75a0deb3347037042d (diff) | |
download | qtlocation-mapboxgl-3c488e05efdb9415e658fa820504692b906c3443.tar.gz |
[ios] Adds thread safe access to MGLNetworkConfiguration events dictionary (#15113)
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLNetworkConfiguration.m | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m index 40993c97b5..0e5046e7a3 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.m +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -11,6 +11,7 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; @property (strong) NSURLSessionConfiguration *sessionConfig; @property (nonatomic, strong) NSMutableDictionary<NSString *, NSDictionary*> *events; @property (nonatomic, weak) id<MGLNetworkConfigurationMetricsDelegate> metricsDelegate; +@property (nonatomic) dispatch_queue_t eventsQueue; @end @@ -20,6 +21,7 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; if (self = [super init]) { self.sessionConfiguration = nil; _events = [NSMutableDictionary dictionary]; + _eventsQueue = dispatch_queue_create("com.mapbox.network-configuration", DISPATCH_QUEUE_CONCURRENT); } return self; @@ -65,8 +67,9 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; } - (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType { - if (urlString && ![self.events objectForKey:urlString]) { - [self.events setObject:@{ MGLStartTime: [NSDate date], MGLResourceType: resourceType } forKey:urlString]; + if (urlString && ![self eventDictionaryForKey:urlString]) { + NSDate *startDate = [NSDate date]; + [self setEventDictionary:@{ MGLStartTime: startDate, MGLResourceType: resourceType } forKey:urlString]; } } @@ -82,10 +85,13 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; { if ([response isKindOfClass:[NSURLResponse class]]) { NSString *urlString = response.URL.relativePath; - if (urlString && [self.events objectForKey:urlString]) { + if (urlString && [self eventDictionaryForKey:urlString]) { NSDictionary *eventAttributes = [self eventAttributesForURL:response withAction:action]; - [self.metricsDelegate networkConfiguration:self didGenerateMetricEvent:eventAttributes]; - [self.events removeObjectForKey:urlString]; + [self removeEventDictionaryForKey:urlString]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self.metricsDelegate networkConfiguration:self didGenerateMetricEvent:eventAttributes]; + }); } } @@ -94,7 +100,7 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; - (NSDictionary *)eventAttributesForURL:(NSURLResponse *)response withAction:(NSString *)action { NSString *urlString = response.URL.relativePath; - NSDictionary *parameters = [self.events objectForKey:urlString]; + NSDictionary *parameters = [self eventDictionaryForKey:urlString]; NSDate *startDate = [parameters objectForKey:MGLStartTime]; NSDate *endDate = [NSDate date]; NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate]; @@ -129,4 +135,28 @@ NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; }; } +#pragma mark - Events dictionary access + +- (nullable NSDictionary*)eventDictionaryForKey:(nonnull NSString*)key { + __block NSDictionary *dictionary; + + dispatch_sync(self.eventsQueue, ^{ + dictionary = [self.events objectForKey:key]; + }); + + return dictionary; +} + +- (void)setEventDictionary:(nonnull NSDictionary*)dictionary forKey:(nonnull NSString*)key { + dispatch_barrier_async(self.eventsQueue, ^{ + [self.events setObject:dictionary forKey:key]; + }); +} + +- (void)removeEventDictionaryForKey:(nonnull NSString*)key { + dispatch_barrier_async(self.eventsQueue, ^{ + [self.events removeObjectForKey:key]; + }); +} + @end |