Gostony McMullen, LLC
For those who like their code done right.

Dev Blog

Here is where you'll find musings about various tips & tricks our developers have used to make their lives easier.

Using blocks to execute code a few microseconds in the future

posted Jun 11th 2011

Here's a neat alternative to using performSelector:withObject:afterDelay::

@implementation NSObject (PerformBlockAfterDelay)

- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay

{

block = [[block copy] autorelease];

[self performSelector:@selector(fireBlockAfterDelay:)

withObject:block

afterDelay:delay];

}

- (void)fireBlockAfterDelay:(void (^)(void))block {

block();

}

@end

Then, when you want to run the code in the future, you just call [self performBlock:^(){ /* some code */ } afterDelay:.5]; The advantage here becomes that you can avoid having to create a target class & method just to do a quick delay.

Made in the USA.