Skip to content

Instantly share code, notes, and snippets.

@furkanmustafa
Last active December 17, 2015 04:48
Show Gist options
  • Save furkanmustafa/5552878 to your computer and use it in GitHub Desktop.
Save furkanmustafa/5552878 to your computer and use it in GitHub Desktop.
simple function for running UI Updates (or anything) on main thread for Cocoa. Can be useful if you have a callback you are not sure it always runs on main thread.
void onMainQueue(void(^block)(void)) {
if ([NSThread isMainThread])
block();
else
dispatch_sync(dispatch_get_main_queue(), block);
}
//usage example:
- (void)callbackThatIDontKnowWhichThreadIsItRunningOn {
onMainQueue(^{
[UIView beginAnimations:nil context:nil];
self.alpha = 0;
[UIView commitAnimations];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment