From 4408f4260b8beb60b138e13ca548dd87a7191458 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 25 Mar 2015 20:35:51 -0500 Subject: [PATCH 1/6] #1109 - Wrapping gesture event logic in background queue --- platform/ios/MGLMapView.mm | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 30edd8797..63f0fcdd1 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -987,16 +987,18 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; - (void) trackGestureEvent:(NSString *) gesture forRecognizer:(UIGestureRecognizer *) recognizer { - // Send Map Zoom Event - CGPoint ptInView = CGPointMake([recognizer locationInView:recognizer.view].x, [recognizer locationInView:recognizer.view].y); - CLLocationCoordinate2D coord = [self convertPoint:ptInView toCoordinateFromView:recognizer.view]; - NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; - [dict setValue:[[NSNumber alloc] initWithDouble:coord.latitude] forKey:@"lat"]; - [dict setValue:[[NSNumber alloc] initWithDouble:coord.longitude] forKey:@"lng"]; - [dict setValue:[[NSNumber alloc] initWithDouble:[self zoomLevel]] forKey:@"zoom"]; - [dict setValue:gesture forKey:@"gesture"]; - - [[MGLMapboxEvents sharedManager] pushEvent:@"map.click" withAttributes:dict]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + // Send Map Zoom Event + CGPoint ptInView = CGPointMake([recognizer locationInView:recognizer.view].x, [recognizer locationInView:recognizer.view].y); + CLLocationCoordinate2D coord = [self convertPoint:ptInView toCoordinateFromView:recognizer.view]; + NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; + [dict setValue:[[NSNumber alloc] initWithDouble:coord.latitude] forKey:@"lat"]; + [dict setValue:[[NSNumber alloc] initWithDouble:coord.longitude] forKey:@"lng"]; + [dict setValue:[[NSNumber alloc] initWithDouble:[self zoomLevel]] forKey:@"zoom"]; + [dict setValue:gesture forKey:@"gesture"]; + + [[MGLMapboxEvents sharedManager] pushEvent:@"map.click" withAttributes:dict]; + }); } #pragma mark - Properties - From dc838a9fc656e4f2d8234d2a1b8fa18319ccdfe7 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 25 Mar 2015 20:42:50 -0500 Subject: [PATCH 2/6] #1109 - Changing priority queue for map.load event. Fixing pushEvent call misplacement. --- platform/ios/MGLMapView.mm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 63f0fcdd1..c10bfb887 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -389,14 +389,12 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; // Fire map.load on a background thread - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSMutableDictionary *evt = [[NSMutableDictionary alloc] init]; [evt setValue:[[NSNumber alloc] initWithDouble:mbglMap->getLatLng().latitude] forKey:@"lat"]; [evt setValue:[[NSNumber alloc] initWithDouble:mbglMap->getLatLng().longitude] forKey:@"lng"]; [evt setValue:[[NSNumber alloc] initWithDouble:mbglMap->getZoom()] forKey:@"zoom"]; - [[MGLMapboxEvents sharedManager] pushEvent:@"map.load" withAttributes:evt]; - [evt setValue:[[NSNumber alloc] initWithBool:[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]] forKey:@"enabled.push"]; NSString *email = @"Unknown"; @@ -407,6 +405,8 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; email = [NSString stringWithFormat:@"%i", sendMail]; } [evt setValue:email forKey:@"enabled.email"]; + + [[MGLMapboxEvents sharedManager] pushEvent:@"map.load" withAttributes:evt]; }); return YES; From 53090afecbeedfd8838b0fc39a8d0f33d6235eb4 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 25 Mar 2015 21:04:12 -0500 Subject: [PATCH 3/6] #1109 - Putting the event generation and process logic for pushEvent into a serial background thread. --- platform/ios/MGLMapboxEvents.m | 78 ++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 5a9bd9455..789ac4d10 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -132,43 +132,47 @@ NSNumber *scale; return; } - NSMutableDictionary *evt = [[NSMutableDictionary alloc] init]; - // mapbox-events stock attributes - [evt setObject:event forKey:@"event"]; - [evt setObject:[NSNumber numberWithInt:1] forKey:@"version"]; - [evt setObject:[self formatDate:[NSDate date]] forKey:@"created"]; - [evt setObject:self.instance forKey:@"instance"]; - [evt setObject:self.anonid forKey:@"anonid"]; - - // mapbox-events-ios stock attributes - [evt setValue:[rfc3339DateFormatter stringFromDate:[NSDate date]] forKey:@"created"]; - [evt setValue:model forKey:@"model"]; - [evt setValue:iOSVersion forKey:@"operatingSystem"]; - [evt setValue:[self getDeviceOrientation] forKey:@"orientation"]; - [evt setValue:[[NSNumber alloc] initWithFloat:(100 * [UIDevice currentDevice].batteryLevel)] forKey:@"batteryLevel"]; - [evt setValue:scale forKey:@"resolution"]; - [evt setValue:carrier forKey:@"carrier"]; - [evt setValue:[self getCurrentCellularNetworkConnectionType] forKey:@"cellularNetworkType"]; - [evt setValue:[self getWifiNetworkName] forKey:@"wifi"]; - [evt setValue:[NSNumber numberWithInt:[self getContentSizeScale]] forKey:@"accessibilityFontScale"]; - - for (NSString *key in [attributeDictionary allKeys]) { - [evt setObject:[attributeDictionary valueForKey:key] forKey:key]; - } - - // Make Immutable Version - NSDictionary *finalEvent = [NSDictionary dictionaryWithDictionary:evt]; - - // Put On The Queue - [self.queue addObject:finalEvent]; - - // Has Flush Limit Been Reached? - if ((int)_queue.count >= (int)_flushAt) { - [self flush]; - } - - // Reset Timer (Initial Starting of Timer after first event is pushed) - [self startTimer]; + dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + + NSMutableDictionary *evt = [[NSMutableDictionary alloc] init]; + // mapbox-events stock attributes + [evt setObject:event forKey:@"event"]; + [evt setObject:[NSNumber numberWithInt:1] forKey:@"version"]; + [evt setObject:[self formatDate:[NSDate date]] forKey:@"created"]; + [evt setObject:self.instance forKey:@"instance"]; + [evt setObject:self.anonid forKey:@"anonid"]; + + // mapbox-events-ios stock attributes + [evt setValue:[rfc3339DateFormatter stringFromDate:[NSDate date]] forKey:@"created"]; + [evt setValue:model forKey:@"model"]; + [evt setValue:iOSVersion forKey:@"operatingSystem"]; + [evt setValue:[self getDeviceOrientation] forKey:@"orientation"]; + [evt setValue:[[NSNumber alloc] initWithFloat:(100 * [UIDevice currentDevice].batteryLevel)] forKey:@"batteryLevel"]; + [evt setValue:scale forKey:@"resolution"]; + [evt setValue:carrier forKey:@"carrier"]; + [evt setValue:[self getCurrentCellularNetworkConnectionType] forKey:@"cellularNetworkType"]; + [evt setValue:[self getWifiNetworkName] forKey:@"wifi"]; + [evt setValue:[NSNumber numberWithInt:[self getContentSizeScale]] forKey:@"accessibilityFontScale"]; + + for (NSString *key in [attributeDictionary allKeys]) { + [evt setObject:[attributeDictionary valueForKey:key] forKey:key]; + } + + // Make Immutable Version + NSDictionary *finalEvent = [NSDictionary dictionaryWithDictionary:evt]; + + // Put On The Queue + [self.queue addObject:finalEvent]; + + // Has Flush Limit Been Reached? + if ((int)_queue.count >= (int)_flushAt) { + [self flush]; + } + + // Reset Timer (Initial Starting of Timer after first event is pushed) + [self startTimer]; + + }); } - (void) flush { From b06e8c8dfcdd6d75f07949e1256b4a2f8e6f552e Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 25 Mar 2015 21:07:08 -0500 Subject: [PATCH 4/6] #1109 - Wrapping event flush method in sync background thread. --- platform/ios/MGLMapboxEvents.m | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 789ac4d10..838fd11d3 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -180,24 +180,26 @@ NSNumber *scale; return; } - int upper = (int)_flushAt; - if (_flushAt > [_queue count]) { - if ([_queue count] == 0) { - return; + dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + + int upper = (int)_flushAt; + if (_flushAt > [_queue count]) { + if ([_queue count] == 0) { + return; + } + upper = (int)[_queue count]; } - upper = (int)[_queue count]; - } - // Create Array of Events to push to the Server - NSRange theRange = NSMakeRange(0, upper); - NSArray *events = [_queue subarrayWithRange:theRange]; + // Create Array of Events to push to the Server + NSRange theRange = NSMakeRange(0, upper); + NSArray *events = [_queue subarrayWithRange:theRange]; - // Update Queue to remove events sent to server - [_queue removeObjectsInRange:theRange]; - - // Send Array of Events to Server - [self postEvents:events]; + // Update Queue to remove events sent to server + [_queue removeObjectsInRange:theRange]; + // Send Array of Events to Server + [self postEvents:events]; + }); } - (void) postEvents:(NSArray *)events { From aa1eabe2305f5e44d573292bc965f63c85f96f40 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 25 Mar 2015 21:53:01 -0500 Subject: [PATCH 5/6] #1109 - Converting pushEvent and flush to use actual serial queues that are dispatched asynchronously. --- platform/ios/MGLMapboxEvents.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 838fd11d3..9ceac3902 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -23,6 +23,8 @@ @property (atomic) NSString *anonid; @property (atomic) NSTimer *timer; @property (atomic) NSString *userAgent; +@property (atomic) dispatch_queue_t serialqPush; +@property (atomic) dispatch_queue_t serialqFlush; @end @@ -57,6 +59,8 @@ NSNumber *scale; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister]; } + _serialqPush = dispatch_queue_create("SERIALQPUSH", DISPATCH_QUEUE_SERIAL); + _serialqFlush = dispatch_queue_create("SERIALQFLUSH", DISPATCH_QUEUE_SERIAL); // Configure Events Infrastructure _queue = [[NSMutableArray alloc] init]; @@ -132,7 +136,7 @@ NSNumber *scale; return; } - dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + dispatch_async(_serialqPush, ^{ NSMutableDictionary *evt = [[NSMutableDictionary alloc] init]; // mapbox-events stock attributes @@ -180,7 +184,7 @@ NSNumber *scale; return; } - dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + dispatch_async(_serialqFlush, ^{ int upper = (int)_flushAt; if (_flushAt > [_queue count]) { From b04f68be9108b0e198608fda76e892dd1d3a5015 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 25 Mar 2015 21:18:11 -0700 Subject: [PATCH 6/6] unique queue names per process & with bundle ID --- platform/ios/MGLMapboxEvents.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 9ceac3902..8a037f18f 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -59,8 +59,10 @@ NSNumber *scale; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister]; } - _serialqPush = dispatch_queue_create("SERIALQPUSH", DISPATCH_QUEUE_SERIAL); - _serialqFlush = dispatch_queue_create("SERIALQFLUSH", DISPATCH_QUEUE_SERIAL); + NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier]; + NSString *uniqueID = [[NSProcessInfo processInfo] globallyUniqueString]; + _serialqPush = dispatch_queue_create([[NSString stringWithFormat:@"%@.%@.SERIALQPUSH", bundleID, uniqueID] UTF8String], DISPATCH_QUEUE_SERIAL); + _serialqFlush = dispatch_queue_create([[NSString stringWithFormat:@"%@.%@.SERIALQFLUSH", bundleID, uniqueID] UTF8String], DISPATCH_QUEUE_SERIAL); // Configure Events Infrastructure _queue = [[NSMutableArray alloc] init];