From 4092e469829e7cc14e946b2ed4e413091bd63b3b Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Mon, 6 Apr 2015 23:03:27 -0500 Subject: #1216 - Add pause and resume methods with corresponding isPaused boolean flag property --- include/mbgl/ios/MGLMapboxEvents.h | 2 ++ platform/ios/MGLMapboxEvents.m | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/mbgl/ios/MGLMapboxEvents.h b/include/mbgl/ios/MGLMapboxEvents.h index e51371368d..2af3d8b168 100644 --- a/include/mbgl/ios/MGLMapboxEvents.h +++ b/include/mbgl/ios/MGLMapboxEvents.h @@ -29,6 +29,8 @@ extern NSString *const MGLEventGestureRotateStart; + (void) setToken:(NSString *)token; + (void) setAppName:(NSString *)appName; + (void) setAppVersion:(NSString *)appVersion; ++ (void) pauseMetricsCollection; ++ (void) resumeMetricsCollection; // You can call this method from any thread. Significant work will // be dispatched to a low-priority background queue and all diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 2dfecbd778..593a983904 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -71,6 +71,11 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; @property (atomic) NSDateFormatter *rfc3339DateFormatter; @property (atomic) CGFloat scale; + +// The isPaused state tracker is only ever accessed from the main thread. +// +@property (nonatomic) BOOL isPaused; + // The timer is only ever accessed from the main thread. // @property (nonatomic) NSTimer *timer; @@ -168,6 +173,8 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Clear Any System TimeZone Cache [NSTimeZone resetSystemTimeZone]; [_rfc3339DateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; + + _isPaused = NO; } return self; } @@ -219,6 +226,20 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; [MGLMapboxEvents sharedManager].appVersion = appVersion; } +// Must be called from the main thread. +// ++ (void) pauseMetricsCollection { + assert([[NSThread currentThread] isMainThread]); + [MGLMapboxEvents sharedManager].isPaused = YES; +} + +// Must be called from the main thread. +// ++ (void) resumeMetricsCollection { + assert([[NSThread currentThread] isMainThread]); + [MGLMapboxEvents sharedManager].isPaused = NO; +} + // Can be called from any thread. Can be called rapidly from // the UI thread, so performance is paramount. // -- cgit v1.2.1 From def309ba245635eeff53b3a5fb3b3821272d4d7c Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Mon, 6 Apr 2015 23:15:00 -0500 Subject: #1216 - Adding start and stop location manager update to pause and resume functions. --- include/mbgl/ios/MGLMetricsLocationManager.h | 4 +++- platform/ios/MGLMapboxEvents.m | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/mbgl/ios/MGLMetricsLocationManager.h b/include/mbgl/ios/MGLMetricsLocationManager.h index 7281d05010..5f3b568187 100644 --- a/include/mbgl/ios/MGLMetricsLocationManager.h +++ b/include/mbgl/ios/MGLMetricsLocationManager.h @@ -2,8 +2,10 @@ @interface MGLMetricsLocationManager : NSObject -// This method can be called from any thread. +// These methods can be called from any thread. // + (instancetype)sharedManager; ++ (void) startUpdatingLocation; ++ (void) stopUpdatingLocation; @end diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 593a983904..0f3422b86d 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -231,6 +231,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; + (void) pauseMetricsCollection { assert([[NSThread currentThread] isMainThread]); [MGLMapboxEvents sharedManager].isPaused = YES; + [MGLMetricsLocationManager stopUpdatingLocation]; } // Must be called from the main thread. @@ -238,6 +239,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; + (void) resumeMetricsCollection { assert([[NSThread currentThread] isMainThread]); [MGLMapboxEvents sharedManager].isPaused = NO; + [MGLMetricsLocationManager startUpdatingLocation]; } // Can be called from any thread. Can be called rapidly from @@ -268,6 +270,11 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; return; } + // Metrics Collection Has Been Paused + if (_isPaused) { + return; + } + if (!event) return; NSMutableDictionary *evt = [[NSMutableDictionary alloc] initWithDictionary:attributeDictionary]; -- cgit v1.2.1 From e86597011f83022fd534c7cac2afe058b386b4b2 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Tue, 7 Apr 2015 15:53:17 -0500 Subject: #1216 - Added test usage of pausing / resuming Metrics to simulate app entering background mode and no other location manager running. Added paused state checking to pauseMetricsCollection and resumeMetricsCollection --- ios/app/MBXAppDelegate.m | 12 ++++++++++++ platform/ios/MGLMapboxEvents.m | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m index f0541b9971..8c5947354a 100644 --- a/ios/app/MBXAppDelegate.m +++ b/ios/app/MBXAppDelegate.m @@ -1,5 +1,6 @@ #import "MBXAppDelegate.h" #import "MBXViewController.h" +#import @implementation MBXAppDelegate @@ -12,4 +13,15 @@ return YES; } +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + [MGLMapboxEvents resumeMetricsCollection]; +} + +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + [MGLMapboxEvents pauseMetricsCollection]; +} + + @end diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 0f3422b86d..346bd70aca 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -230,6 +230,9 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // + (void) pauseMetricsCollection { assert([[NSThread currentThread] isMainThread]); + if ([MGLMapboxEvents sharedManager].isPaused) { + return; + } [MGLMapboxEvents sharedManager].isPaused = YES; [MGLMetricsLocationManager stopUpdatingLocation]; } @@ -238,6 +241,9 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // + (void) resumeMetricsCollection { assert([[NSThread currentThread] isMainThread]); + if (![MGLMapboxEvents sharedManager].isPaused) { + return; + } [MGLMapboxEvents sharedManager].isPaused = NO; [MGLMetricsLocationManager startUpdatingLocation]; } -- cgit v1.2.1 From ec0052bd4ecf3be7f4c46474df8bff1e7293d92e Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Tue, 7 Apr 2015 16:18:38 -0500 Subject: #1216 - Documenting use of pauseMetricsCollection and resumeMetricsCollection --- ios/app/MBXAppDelegate.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m index 8c5947354a..75e02c9980 100644 --- a/ios/app/MBXAppDelegate.m +++ b/ios/app/MBXAppDelegate.m @@ -15,11 +15,21 @@ - (void)applicationWillEnterForeground:(UIApplication *)application { + // Example of how to resume Metrics Collection + + // Reasons for needing to resume: + // 1. In UIBackground and app starts listening for Location Updates where it previously had not been listening. + // 2. App is entering foreground where it had called pauseMetricsCollection. [MGLMapboxEvents resumeMetricsCollection]; } - (void)applicationDidEnterBackground:(UIApplication *)application { + // Example of how to pause Metrics Collection + + // Reason for needing to pause: + // 1. Either entering or already in UIBackground and app stops listening for Location Updates + // via any CLLocationManager instance it may have. [MGLMapboxEvents pauseMetricsCollection]; } -- cgit v1.2.1 From da106045f4f6bd6153e7411a33d474b9c1450f96 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Tue, 7 Apr 2015 16:19:33 -0500 Subject: #1216 - Commenting out pause / resume test hooks --- ios/app/MBXAppDelegate.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m index 75e02c9980..f2ef69ec63 100644 --- a/ios/app/MBXAppDelegate.m +++ b/ios/app/MBXAppDelegate.m @@ -13,6 +13,7 @@ return YES; } +/** - (void)applicationWillEnterForeground:(UIApplication *)application { // Example of how to resume Metrics Collection @@ -32,6 +33,6 @@ // via any CLLocationManager instance it may have. [MGLMapboxEvents pauseMetricsCollection]; } - +*/ @end -- cgit v1.2.1