Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created February 10, 2011 21:03
Show Gist options
  • Save agoodman/821337 to your computer and use it in GitHub Desktop.
Save agoodman/821337 to your computer and use it in GitHub Desktop.
GCD Thread Redirecting
/* say you have a process driven by the main thread, such as
* a UI control, and you want the process to execute on a
* background thread, but once it's done, you want to update
* some UI element. that must happen on the main thread, so
* here's an example of how you might do that
*/
- (IBAction)buttonClicked
{
dispatch_async(backgroundProcessingQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
// show activity indicator
[delegate willProcessInBackground]; // if you want to follow Apple's naming convention
});
// perform the long-duration task here
dispatch_async(dispatch_get_main_queue(), ^{
// update UI element
[delegate didProcessInBackground]; // if you want to follow Apple's naming convention
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment