Create a gist now

Instantly share code, notes, and snippets.

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