From ff0ec15e1d3544bd7c2b7aee868fc9017aaba661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Sat, 9 May 2015 00:51:00 -0700 Subject: [PATCH] More cleanup and sanitization Moved constant values from ivars into constants. Invalidate the flush timer and clear the event queue on pause. Rewrote some flush logic to be easier to follow. --- platform/ios/MGLMapboxEvents.m | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 74693f5c0..310283c5c 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -44,6 +44,9 @@ NSString *const MGLEventGesturePanStart = @"Pan"; NSString *const MGLEventGesturePinchStart = @"Pinch"; NSString *const MGLEventGestureRotateStart = @"Rotation"; +const NSUInteger MGLMaximumEventsPerFlush = 20; +const NSTimeInterval MGLFlushInterval = 60; + @interface MGLMapboxEventsData : NSObject // All of the following properties are written to only from @@ -140,7 +143,6 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; @property (atomic) NSString *appName; @property (atomic) NSString *appVersion; @property (atomic) NSString *appBuildNumber; -@property (atomic) NSUInteger flushAt; @property (atomic) NSDateFormatter *rfc3339DateFormatter; @property (atomic) NSURLSession *session; @property (atomic) NSData *digicertCert; @@ -157,10 +159,6 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // @property (nonatomic) NSTimer *timer; -// The flush expiration time is only ever accessed from the main thread. -// -@property (nonatomic) NSTimeInterval flushAfter; - // This is an array of events to push. All access to it will be // from our own serial queue. // @@ -241,8 +239,6 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Events Control _eventQueue = [[NSMutableArray alloc] init]; - _flushAt = 20; - _flushAfter = 60; // Setup Date Format _rfc3339DateFormatter = [[NSDateFormatter alloc] init]; @@ -298,6 +294,9 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; return; } self.paused = YES; + [_timer invalidate]; + _timer = nil; + [_eventQueue removeAllObjects]; _data = nil; [_session invalidateAndCancel]; _session = nil; @@ -415,7 +414,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; [_eventQueue addObject:finalEvent]; // Has Flush Limit Been Reached? - if (_eventQueue.count >= strongSelf.flushAt) { + if (_eventQueue.count >= MGLMaximumEventsPerFlush) { [strongSelf flush]; } else if (_eventQueue.count == 1) { // If this is first new event on queue start timer, @@ -439,12 +438,10 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; dispatch_async(_serialQueue, ^{ MGLMapboxEvents *strongSelf = weakSelf; - if ( ! strongSelf) return; - - __block NSArray *events; + if ( ! strongSelf || [_eventQueue count] == 0) return; // Make an immutable copy - events = [NSArray arrayWithArray:_eventQueue]; + NSArray *events = [NSArray arrayWithArray:_eventQueue]; // Update Queue to remove events sent to server [_eventQueue removeAllObjects]; @@ -494,14 +491,11 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; - (void) startTimer { void (^timerBlock)() = ^{ // Stop Timer if it already exists - if (_timer) { - [_timer invalidate]; - _timer = nil; - } + [_timer invalidate]; // Start New Timer - _timer = [NSTimer scheduledTimerWithTimeInterval:_flushAfter - target:[self class] + _timer = [NSTimer scheduledTimerWithTimeInterval:MGLFlushInterval + target:self selector:@selector(flush) userInfo:nil repeats:YES];