Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
fix dispatch-thread pattern
Browse files Browse the repository at this point in the history
 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.
  • Loading branch information
incanus committed May 13, 2015
1 parent 1c8d3c5 commit 190e911
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
36 changes: 18 additions & 18 deletions platform/ios/MGLAccountManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ @interface MGLAccountManager()

@implementation MGLAccountManager

static MGLAccountManager *_sharedManager;

// Can be called from any thread.
//
+ (instancetype) sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
return;

This comment has been minimized.

Copy link
@incanus

incanus May 13, 2015

Author Contributor

WAT

}
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;
}

Expand Down
29 changes: 14 additions & 15 deletions platform/ios/MGLMapboxEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,24 @@ - (instancetype) init {
// 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;
}

Expand Down

0 comments on commit 190e911

Please sign in to comment.