diff options
author | Brad Leege <bleege@gmail.com> | 2015-04-07 20:23:59 -0500 |
---|---|---|
committer | Brad Leege <bleege@gmail.com> | 2015-04-07 20:23:59 -0500 |
commit | 7dc7fbb4958f250e144b2653c20996b6e65cd224 (patch) | |
tree | a8ab44540d1548a74d89b4097edace92379b0163 | |
parent | ce32e9f92ba5e9994ea1e8fb4dc09275db193ab2 (diff) | |
parent | da106045f4f6bd6153e7411a33d474b9c1450f96 (diff) | |
download | qtlocation-mapboxgl-7dc7fbb4958f250e144b2653c20996b6e65cd224.tar.gz |
Merge pull request #1221 from mapbox/1216-pause
Pause and Resume Metrics
-rw-r--r-- | include/mbgl/ios/MGLMapboxEvents.h | 2 | ||||
-rw-r--r-- | include/mbgl/ios/MGLMetricsLocationManager.h | 4 | ||||
-rw-r--r-- | ios/app/MBXAppDelegate.m | 23 | ||||
-rw-r--r-- | platform/ios/MGLMapboxEvents.m | 34 |
4 files changed, 62 insertions, 1 deletions
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/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/ios/app/MBXAppDelegate.m b/ios/app/MBXAppDelegate.m index f0541b9971..f2ef69ec63 100644 --- a/ios/app/MBXAppDelegate.m +++ b/ios/app/MBXAppDelegate.m @@ -1,5 +1,6 @@ #import "MBXAppDelegate.h" #import "MBXViewController.h" +#import <mbgl/ios/MGLMapboxEvents.h> @implementation MBXAppDelegate @@ -12,4 +13,26 @@ return YES; } +/** +- (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]; +} +*/ + @end diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 2dfecbd778..346bd70aca 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,28 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; [MGLMapboxEvents sharedManager].appVersion = appVersion; } +// Must be called from the main thread. +// ++ (void) pauseMetricsCollection { + assert([[NSThread currentThread] isMainThread]); + if ([MGLMapboxEvents sharedManager].isPaused) { + return; + } + [MGLMapboxEvents sharedManager].isPaused = YES; + [MGLMetricsLocationManager stopUpdatingLocation]; +} + +// Must be called from the main thread. +// ++ (void) resumeMetricsCollection { + assert([[NSThread currentThread] isMainThread]); + if (![MGLMapboxEvents sharedManager].isPaused) { + return; + } + [MGLMapboxEvents sharedManager].isPaused = NO; + [MGLMetricsLocationManager startUpdatingLocation]; +} + // Can be called from any thread. Can be called rapidly from // the UI thread, so performance is paramount. // @@ -247,6 +276,11 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; return; } + // Metrics Collection Has Been Paused + if (_isPaused) { + return; + } + if (!event) return; NSMutableDictionary *evt = [[NSMutableDictionary alloc] initWithDictionary:attributeDictionary]; |