From 87c61657a12f41c8d32c85fc0e0fa525302ed614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Thu, 30 Apr 2015 11:35:52 -0400 Subject: [PATCH] Objective-C niceties Added macro for main thread assertions based on `NSAssert()`. Reworded Boolean property per Objective-C conventions. --- gyp/platform-ios.gypi | 1 + platform/ios/MGLMapView.mm | 3 ++- platform/ios/MGLMapboxEvents.m | 31 ++++++++++++------------- platform/ios/NSException+MGLAdditions.h | 3 +++ 4 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 platform/ios/NSException+MGLAdditions.h diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index cc4c1349b..161b3fb70 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -33,6 +33,7 @@ '../include/mbgl/ios/MGLTypes.h', '../platform/ios/NSBundle+MGLAdditions.h', '../platform/ios/NSBundle+MGLAdditions.m', + '../platform/ios/NSException+MGLAdditions.m', '../platform/ios/NSProcessInfo+MGLAdditions.h', '../platform/ios/NSProcessInfo+MGLAdditions.m', '../platform/ios/NSString+MGLAdditions.h', diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 3ee6ed091..542838797 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -20,6 +20,7 @@ #import "NSBundle+MGLAdditions.h" #import "NSString+MGLAdditions.h" #import "NSProcessInfo+MGLAdditions.h" +#import "NSException+MGLAdditions.h" #import "MGLAnnotation.h" #import "MGLUserLocationAnnotationView.h" #import "MGLUserLocation_Private.h" @@ -2348,7 +2349,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)invalidate { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); [self.glView setNeedsDisplay]; diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index cf8345222..dcf1d0bab 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -8,6 +8,7 @@ #import "MGLMetricsLocationManager.h" #import "NSProcessInfo+MGLAdditions.h" #import "NSBundle+MGLAdditions.h" +#import "NSException+MGLAdditions.h" #include @@ -83,9 +84,9 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; @property (atomic) NSData *geoTrustCert; -// The isPaused state tracker is only ever accessed from the main thread. +// The paused state tracker is only ever accessed from the main thread. // -@property (nonatomic) BOOL isPaused; +@property (nonatomic, getter=isPaused) BOOL paused; // The timer is only ever accessed from the main thread. // @@ -111,7 +112,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Must be called from the main thread. Only called internally. // - (instancetype) init { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); self = [super init]; if (self) { @@ -207,8 +208,6 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Enable Battery Monitoring [UIDevice currentDevice].batteryMonitoringEnabled = YES; - - _isPaused = NO; } return self; } @@ -242,39 +241,39 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Must be called from the main thread. // + (void) setToken:(NSString *)token { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); [MGLMapboxEvents sharedManager].token = token; } // Must be called from the main thread. // + (void) setAppName:(NSString *)appName { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); [MGLMapboxEvents sharedManager].appName = appName; } // Must be called from the main thread. // + (void) setAppVersion:(NSString *)appVersion { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); [MGLMapboxEvents sharedManager].appVersion = appVersion; } // Must be called from the main thread. // + (void) setAppBuildNumber:(NSString *)appBuildNumber { - assert([[NSThread currentThread] isMainThread]); + MGLAssertIsMainThread(); [MGLMapboxEvents sharedManager].appBuildNumber = appBuildNumber; } // Must be called from the main thread. // + (void) pauseMetricsCollection { - assert([[NSThread currentThread] isMainThread]); - if ([MGLMapboxEvents sharedManager].isPaused) { + MGLAssertIsMainThread(); + if ([MGLMapboxEvents sharedManager].paused) { return; } - [MGLMapboxEvents sharedManager].isPaused = YES; + [MGLMapboxEvents sharedManager].paused = YES; [MGLMetricsLocationManager stopUpdatingLocation]; [MGLMetricsLocationManager stopMonitoringVisits]; } @@ -282,11 +281,11 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; // Must be called from the main thread. // + (void) resumeMetricsCollection { - assert([[NSThread currentThread] isMainThread]); - if (![MGLMapboxEvents sharedManager].isPaused) { + MGLAssertIsMainThread(); + if (![MGLMapboxEvents sharedManager].paused) { return; } - [MGLMapboxEvents sharedManager].isPaused = NO; + [MGLMapboxEvents sharedManager].paused = NO; [MGLMetricsLocationManager startUpdatingLocation]; [MGLMetricsLocationManager startMonitoringVisits]; } @@ -323,7 +322,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; } // Metrics Collection Has Been Paused - if (_isPaused) { + if (_paused) { return; } diff --git a/platform/ios/NSException+MGLAdditions.h b/platform/ios/NSException+MGLAdditions.h new file mode 100644 index 000000000..4d5de1511 --- /dev/null +++ b/platform/ios/NSException+MGLAdditions.h @@ -0,0 +1,3 @@ +#import + +#define MGLAssertIsMainThread() NSAssert([[NSThread currentThread] isMainThread], @"-[%@ %@] must be accessed on the main thread, not %@", [self class], NSStringFromSelector(_cmd), [NSThread currentThread])