Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer doesn't stop

I've got a timer which fires when the viewWillAppear method is being called and invalidates when the viewDidDisappear method is being called. But after a certain amount of switching between views the timer continues firing even after it was invalidated. What's the problem?

Here is my code:

NSTimer *timer;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f
                     target: self
                     selector:@selector( timerAction )
                     userInfo:nil
                     repeats:YES];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [timer invalidate];
    timer = nil;
}

-(void) timerAction
{
    NSLog(@"timerAction");
}
like image 775
Ilya Suzdalnitski Avatar asked Jun 23 '09 09:06

Ilya Suzdalnitski


4 Answers

I should have been keeping a link to the timer by retaining it. :) I've asked this question 5 months ago, and it's amazing, how much more experience I acquired. :)

timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
...
...
[timer invalidate];
[timer release];
like image 92
Ilya Suzdalnitski Avatar answered Nov 12 '22 19:11

Ilya Suzdalnitski


This is an absolute solution. None of the ones above worked for me, so I did this,

where you want to start the timer,

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:NO];

The method that the timer calls,

-(void)timerMethod
{
    // do what you need to do

    if (needs to be called again) {

        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:NO];        
    }
}

hope this helps,

like image 40
Mona Avatar answered Nov 12 '22 21:11

Mona


A method called by a timer should have the definition

- (void)methodName:(NSTimer *)aTimer;

This way the method has the instance of timer which was fired. The way you are doing it, the method will not know whether the timer was invalidated or not.

Try changing your timer initialization to

timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f target: self selector:@selector(timerAction:) userInfo:nil repeats:YES]

and the method to

-(void) timerAction:(NSTimer *)aTimer{...}

Hope that helps

like image 6
lostInTransit Avatar answered Nov 12 '22 20:11

lostInTransit


This may not be the issue, but you need to retain the reference to timer that is returned from scheduledTimerWithInterval:. Without doing this, your pointer to the timer might be invalid by the time you go to stop it.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    timer = [[NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(timerAction) userInfo:nil repeats:YES] retain];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [timer invalidate];
    [timer release];
    timer = nil;
    [super viewDidDisappear:animated];
}

- (void)dealloc
{
    [timer invalidate];
    [timer release];
    timer = nil;
    [super dealloc];
}

Also, try setting a breakpoint in viewDidDisappear and make sure it's getting called!

like image 2
Ben Gotow Avatar answered Nov 12 '22 20:11

Ben Gotow