summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLNetworkConfiguration.m
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin/src/MGLNetworkConfiguration.m')
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration.m42
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