How to check if NSTimer has been already invalidated

跟風遠走 提交于 2019-12-04 00:35:59

Once you invalidate the timer, simply call release on it (assuming you've retained the reference you're holding on to) and then nil out your reference. That way when you exit the view, trying to invalidate the timer a second time will just call that method on nil instead, which does nothing.

Alternatively, you can use -[NSTimer isValid] to check if it's valid before invalidating, but there's really no reason to hold onto your reference after invalidating it the first time anyway. Also, if your real problem is that you haven't retained your reference and so the first -invalidate actually leaves you with a reference pointing at a released object, then calling -isValid won't help anyway.

Ben Gottlieb

What I usually do is nil-out the member variable holding the timer, either when it fires, or when I invalidate it. Then you can just do:

[myNSTimer invalidate]; 
myNSTimer = nil;

and it'll do the right thing.

I have had the same problem while I was trying to pause & restart a timer. To stop it:

[timer invalidate];
[timer release];
timer = nil;

And to start/ restart it:

timer = [[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(aSelector) userInfo:nil repeats:YES] retain];

Now it doesn't crash when I invalidate it.

Swift:

if timer.valid {
   timer.invalidate()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!