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.18. Exiting Threads and Timers

5.18. Exiting Threads and Timers

Problem

You would like to stop a thread or a timer, or prevent one from firing again.

Solution

For timers, use the invalidate instance method of NSTimer. For threads, use the cancel method. Avoid using the exit method of threads, as it does not give the thread a chance to clean up after itself and your application will end up leaking resources:

NSThread *thread = /* Get the reference to your thread here */;
[thread cancel];

NSTimer *timer = /* Get the reference to your timer here */;
[timer invalidate];

Discussion

Exiting a timer is quite straightforward; you can simply call the timer’s invalidate instance method. After you call that method, the timer will not fire any more events to its target object.


  

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


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