4thSpace
4thSpace

Reputation: 44312

Is NSTimer auto retained?

I have a -(void)save method that is called when a user clicks a navigation bar button. In that method is the following NSTimer:

[NSTimer scheduledTimerWithTimeInterval:.25f target:self selector:@selector(flashBackgroundRed) userInfo: nil repeats: flashRepeat];

The timer repeats 4 times since the flashBackgroundRed keeps a count and sets flashRepeat to NO after 4 hits. All works well the first time a user clicks to save. But if the user keeps hitting save with incorrect data, I keep them on the current view. For some reason, flashes get increasing faster. As if each one is 25% of the previous one's interval. I'm not retaining this timer. It only lives in the save method.

If the user clicks the cancel button, viewB (one with NSTimer) is popped and viewA displays. Going from viewA back to viewB seems to reset the timer invterval. But the increasingly fast flashes cycle starts again. Any ideas?

Upvotes: 1

Views: 1424

Answers (2)

sbooth
sbooth

Reputation: 16976

It sounds like you are scheduling more timers than you intend to.

Once scheduled, timers are retained until invalidated. Repeating timers must be manually invalidated.

The repeats argument specifies if the timer will repeat. You can't specify how many times a timer should repeat, only whether or not it will.

Also, the method signature for your selector is wrong. It should look like

-(void)timerFireMethod:(NSTimer*)theTimer

In your timer callback you can determine if you want the timer to continue repeating or not; if not, call [timer invalidate].

Upvotes: 1

Chuck
Chuck

Reputation: 237040

NSTimer is retained by the run loop as long as it is scheduled.

It sounds like the problem is that you keep creating equivalent repeating timers at slightly different times, so they visually mesh together into one effect with a different frequency than you want. Try storing a reference to the timer and invalidating it when you're going to create a new one.

Upvotes: 1

Related Questions