Created
February 10, 2011 21:03
-
-
Save agoodman/821337 to your computer and use it in GitHub Desktop.
GCD Thread Redirecting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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