EXC_BAD_ACCESS dropping NSTimer

I want to reset two NSTimers with a new TimeInterval. There would be no problem if I knew if they were released before I restarted them.

I can’t work like this:

[timer invalidate]; if(startTimers == YES) timer = [NSTimer scheduledTimerWithTimerInterval:...] 

because I don’t know if the timer was invalid before I invalidated it.

And if I cancel the freed timer (if the timer is invalidated), I get EXC_BAD_ACCESS.

+4
source share
2 answers

When you release the timer, also set its value to zero. Then [timer invalidate] will silently do nothing if the timer is zero.

 [timer invalidate]; timer = nil; 
+14
source

I already use Brian and still get EXC_BAD_ACCESS .

For me, a dispatch_after solved the problem:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){ if ([_timer isValid]) [_timer invalidate]; _timer = nil; [self timer]; }); 

But now it looks a little dirty, I know ...

+1
source

Source: https://habr.com/ru/post/1335438/


All Articles