Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, December 17, 2013

iOS Run on Main Thread After Delay

So, you want to run some code on the main thread after a short delay, eh?


Sometimes we want to delay execution of code for a space, maybe because we want to put up a wait indicator and let the user know something interesting is happening. No matter your reasons, delaying the execution of code is often an acceptable way to get around issues[1]. Here's a quick example of how to accomplish this:

  int delaySeconds = 10;
  dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delaySeconds * NSEC_PER_SEC);
  dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){
    NSLog(@"This is running in the main thread after 10 seconds!");
  });

There you go, and best of luck to you on your journey!

[1] However, it can also be a signal of a poor implementation, so use such things as delaying code in a VERY sparing fashion.




About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates