• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTimer & Runloop questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTimer & Runloop questions


  • Subject: Re: NSTimer & Runloop questions
  • From: Ken Thomases <email@hidden>
  • Date: Thu, 19 Mar 2009 23:01:22 -0500

On Mar 19, 2009, at 1:51 PM, Robbie Hanson wrote:

However I'm not sure how safe it is to use the setFireDate method of NSTimer. Consider the following code:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:NO];


[NSThread sleepForTimeInterval:2.0];

// This line will properly prevent the timer from firing
//[timer invalidate];

// This line will NOT
[timer setFireDate:[NSDate distantFuture]];

Why is this exactly?

At a guess, because the timer has already fired. You are changing the time when it will _next_ fire. But the timer is already marked as having fired. When the run loop next gets a chance, it will invoke the callback for the previous firing. Invalidating the timer actually removes it from the run loop, preventing that.


Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSTimer & Runloop questions
      • From: Robbie Hanson <email@hidden>
References: 
 >NSTimer & Runloop questions (From: Robbie Hanson <email@hidden>)

  • Prev by Date: Banding/smoothing problem with drawing angled gradients.
  • Next by Date: Re: Banding/smoothing problem with drawing angled gradients.
  • Previous by thread: NSTimer & Runloop questions
  • Next by thread: Re: NSTimer & Runloop questions
  • Index(es):
    • Date
    • Thread