GCD Delayed Block Invocation using dispatch_after

I needed to run some code after a period of time after an asynchronous remoting call completed. Normally you’d use NSObject’s performSelector:withObject:afterDelay:, but I wanted to use blocks instead so the code could be inline. Grand Central Dispatch’s dispatch_after method did exactly what I needed.

Try it out:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), 
                   dispatch_get_main_queue(), ^{
       // This gets called after 1 second
    }); 
Advertisements