Skip to content

Instantly share code, notes, and snippets.

@xtinger
Created May 10, 2016 13:34
Show Gist options
  • Save xtinger/7f9981bcb4f213c7897f66847daf6f18 to your computer and use it in GitHub Desktop.
Save xtinger/7f9981bcb4f213c7897f66847daf6f18 to your computer and use it in GitHub Desktop.
dispatch_once_on_main_thread
void dispatch_once_on_main_thread(dispatch_once_t *predicate,
dispatch_block_t block) {
if ([NSThread isMainThread]) {
dispatch_once(predicate, block);
} else {
if (DISPATCH_EXPECT(*predicate == 0L, NO)) {
dispatch_sync(dispatch_get_main_queue(), ^{
dispatch_once(predicate, block);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment