Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created May 10, 2011 06:16
Show Gist options
  • Select an option

  • Save chrishulbert/963992 to your computer and use it in GitHub Desktop.

Select an option

Save chrishulbert/963992 to your computer and use it in GitHub Desktop.

Revisions

  1. chrishulbert created this gist May 10, 2011.
    19 changes: 19 additions & 0 deletions gcd.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // 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
    });