Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Concurrency > 5.8. Performing Tasks After a Delay with GCD

5.8. Performing Tasks After a Delay with GCD

Problem

You want to be able to execute code but after a certain amount of delay, which you would like to specify, using GCD.

Solution

Use the dispatch_after and dispatch_after_f functions.

Discussion

With Core Foundation, you can invoke a selector in an object after a given period of time, using the performSelector:withObject:afterDelay: method of the NSObject class. Here is an example:

- (void) printString:(NSString *)paramString{
  NSLog(@"%@", paramString);
}

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  
  [self performSelector:@selector(printString:)
             withObject:@"Grand Central Dispatch"
             afterDelay:3.0];

  self.window = [[UIWindow alloc] initWithFrame:
                 [[UIScreen mainScreen] bounds]];
  
  // Override point for customization after application launch.
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  return YES;
}

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial