Re: NSTimer +timerWithTimeInterval:
Re: NSTimer +timerWithTimeInterval:
- Subject: Re: NSTimer +timerWithTimeInterval:
- From: John Bishop via Cocoa-dev <email@hidden>
- Date: Wed, 29 Apr 2020 17:41:40 -0500
The documentation indicates "... for a repeating timer, you must invalidate the
time object yourself by calling its invalidate method. Calling this method
requests the removal of the timer from the current run loop; as a result, you
should always call the invalidate method from the same thread on which the
timer was installed. Invalidating the timer immediately disables it so that it
no longer affects the run loop. The run loop then removes the timer (and the
strong reference it had to the timer), either just before the invalidate method
returns or at some later point."
> ? If I specify 100.0 for the time interval, my method gets called 5 times/sec.
I think you meant: "my method gets called every 5 seconds" I'm also in
agreement that you might have more timers than you think, but I can be swayed.
Try it with scheduledTimerWithTimeInterval: and instrument its creation (NSLog
or equiv.) which might help debug whether its the timer or how it's added or
perhaps how the caller is threaded...
@property (nonatomic, assign) NSTimer *myOneSecondTimer;
[self.myOneSecondTimer invalidate];
self.myOneSecondTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(newData:) userInfo nil
repeats:YES];
> On Apr 29, 2020, at 4:35 PM, email@hidden wrote:
>
> Subject: NSTimer +timerWithTimeInterval:
> Message-ID:
> <email@hidden>
> Content-Type: text/plain; charset=utf-8
>
> When I issue NSTimer's +timerWithTimeInterval::::: method, I'm getting
> unexpected timer firing times (20X faster than expected).
>
> ? If I specify 1.0 for the time interval, my method gets called 20 times/sec.
> ? If I specify 20.0 for the time interval, my method gets called 1 time/sec.
> ? If I specify 100.0 for the time interval, my method gets called 5 times/sec.
> ...etc.
>
> Here is my only invocation, called once and nevermore:
>
> NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0 // should be
> 1/sec
> target:self
> selector:@selector(newData:)
> userInfo:nil
> repeats:YES];
> [[NSRunLoop mainRunLoop] addTimer:newTimer
> forMode:NSRunLoopCommonModes];
>
> How can this be? NSTimeInterval is supposed to be in seconds!
>
> From the Apple documentation:
>
> + (NSTimer
> <https://developer.apple.com/documentation/foundation/nstimer?language=objc>
> *)timerWithTimeInterval:(NSTimeInterval
> <https://developer.apple.com/documentation/foundation/nstimeinterval?language=objc>)ti
>
> target:(id)aTarget
> selector:(SEL)aSelector
> userInfo:(id)userInfo
> repeats:(BOOL)yesOrNo;
> Parameters
> ti
> The number of seconds between firings of the timer. If ti is less than or
> equal to 0.0, this method chooses the nonnegative value of 0.0001 seconds
> instead. A NSTimeInterval value is always specified in seconds; it yields
> sub-millisecond precision over a range of 10,000 years.
>
> I can accommodate this odd behavior in my code, but something's gotta be
> wrong...
> -Carl
>
------------------------------------------------------------------
John Bishop
Mulligan Software
<mailto:email@hidden>
<https://www.mulligansoftware.com>
_______________________________________________
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