Skip to content

Instantly share code, notes, and snippets.

@rlb3
Forked from chrishulbert/gcd.m
Created May 16, 2011 19:17
Show Gist options
  • Save rlb3/975112 to your computer and use it in GitHub Desktop.
Save rlb3/975112 to your computer and use it in GitHub Desktop.
Tricks with grand central dispatch
// Using grand central dispatch (GCD) so we don't block the GUI
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// Background, long stuff runs here, but doesn't affect the GUI
dispatch_async(dispatch_get_main_queue(), ^{
// The GUI thread stuff goes here
[self.tableView reloadData]; // example
});
});
// Just like javascript's setTimeout: call a block after a delay, on the main thread:
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 0.5); // Make it happen in half a second
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
// This will happen after the delay
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment