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
  • PrintPrint
Share this Page URL
Help

5. Concurrency > 5.15. Creating Timers

5.15. Creating Timers

Problem

You would like to perform a specific task repeatedly with a certain delay. For instance, you want to update a view on your screen every second that your application is running.

Solution

Use a timer:

- (void) paint:(NSTimer *)paramTimer{
  /* Do something here */
  NSLog(@"Painting");
}

- (void) startPainting{
  
  self.paintingTimer = [NSTimer 
                        scheduledTimerWithTimeInterval:1.0
                        target:self
                        selector:@selector(paint:) 
                        userInfo:nil
                        repeats:YES];
  
}

- (void) stopPainting{
  if (self.paintingTimer != nil){
    [self.paintingTimer invalidate];    
  }
}

- (void)applicationWillResignActive:(UIApplication *)application{
  [self stopPainting];
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
  [self startPainting];
}

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint