From 190e91120927ea2e48a72c8337ba923dabe31f85 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Tue, 12 May 2015 18:23:59 -0700 Subject: fix dispatch-thread pattern 1. Always bail early if in IB. 2. Always run on main thread if not already. 3. THEN dispatch_once to create the static instance. --- platform/ios/MGLAccountManager.m | 36 ++++++++++++++++++------------------ platform/ios/MGLMapboxEvents.m | 29 ++++++++++++++--------------- 2 files changed, 32 insertions(+), 33 deletions(-) (limited to 'platform') diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m index 9490e91bae..32540e4e46 100644 --- a/platform/ios/MGLAccountManager.m +++ b/platform/ios/MGLAccountManager.m @@ -14,28 +14,28 @@ @implementation MGLAccountManager -static MGLAccountManager *_sharedManager; - // Can be called from any thread. // + (instancetype) sharedManager { + if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + return; + } static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - if ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { - void (^setupBlock)() = ^{ - _sharedManager = [[self alloc] init]; - _sharedManager.mapboxMetricsEnabledSettingShownInApp = NO; - }; - if ( ! [[NSThread currentThread] isMainThread]) { - dispatch_sync(dispatch_get_main_queue(), ^{ - setupBlock(); - }); - } - else { - setupBlock(); - } - } - }); + static MGLAccountManager *_sharedManager; + void (^setupBlock)() = ^{ + dispatch_once(&onceToken, ^{ + _sharedManager = [[self alloc] init]; + _sharedManager.mapboxMetricsEnabledSettingShownInApp = NO; + }); + }; + if ( ! [[NSThread currentThread] isMainThread]) { + dispatch_sync(dispatch_get_main_queue(), ^{ + setupBlock(); + }); + } + else { + setupBlock(); + } return _sharedManager; } diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index fb94479723..9fcfc71536 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -279,25 +279,24 @@ const NSTimeInterval MGLFlushInterval = 60; // public class convenience methods. May return nil if this feature is disabled. // + (instancetype)sharedManager { + if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + return; + } static dispatch_once_t onceToken; static MGLMapboxEvents *_sharedManager; - dispatch_once(&onceToken, ^{ - if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { - return; - } - - void (^setupBlock)() = ^{ + void (^setupBlock)() = ^{ + dispatch_once(&onceToken, ^{ _sharedManager = [[self alloc] init]; - }; - if ( ! [[NSThread currentThread] isMainThread]) { - dispatch_sync(dispatch_get_main_queue(), ^{ - setupBlock(); - }); - } - else { + }); + }; + if ( ! [[NSThread currentThread] isMainThread]) { + dispatch_sync(dispatch_get_main_queue(), ^{ setupBlock(); - } - }); + }); + } + else { + setupBlock(); + } return _sharedManager; } -- cgit v1.2.1