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

Commit

Permalink
Merge pull request #1112 from mapbox/1109-ui-blocking
Browse files Browse the repository at this point in the history
avoid bogging down UI thread with metrics
  • Loading branch information
incanus committed Mar 26, 2015
2 parents 6e2da81 + b04f68b commit c5eaa32
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 64 deletions.
28 changes: 15 additions & 13 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,12 @@ - (BOOL)commonInit


// 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";
Expand All @@ -413,6 +411,8 @@ - (BOOL)commonInit
email = [NSString stringWithFormat:@"%i", sendMail];
}
[evt setValue:email forKey:@"enabled.email"];

[[MGLMapboxEvents sharedManager] pushEvent:@"map.load" withAttributes:evt];
});

return YES;
Expand Down Expand Up @@ -1032,16 +1032,18 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni

- (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 -
Expand Down
114 changes: 63 additions & 51 deletions platform/ios/MGLMapboxEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ @interface MGLMapboxEvents()
@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

Expand Down Expand Up @@ -57,6 +59,10 @@ - (id) init {

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}
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];
Expand Down Expand Up @@ -132,68 +138,74 @@ - (void) pushEvent:(NSString *)event withAttributes:(NSDictionary *)attributeDic
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_async(_serialqPush, ^{

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 {
if (_token == nil) {
return;
}

int upper = (int)_flushAt;
if (_flushAt > [_queue count]) {
if ([_queue count] == 0) {
return;
}
upper = (int)[_queue count];
}
dispatch_async(_serialqFlush, ^{

// Create Array of Events to push to the Server
NSRange theRange = NSMakeRange(0, upper);
NSArray *events = [_queue subarrayWithRange:theRange];
int upper = (int)_flushAt;
if (_flushAt > [_queue count]) {
if ([_queue count] == 0) {
return;
}
upper = (int)[_queue count];
}

// Update Queue to remove events sent to server
[_queue removeObjectsInRange:theRange];
// Create Array of Events to push to the Server
NSRange theRange = NSMakeRange(0, upper);
NSArray *events = [_queue subarrayWithRange: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 {
Expand Down

0 comments on commit c5eaa32

Please sign in to comment.