NSTimer does not invalidate

岁酱吖の 提交于 2019-12-02 07:25:41

问题


I have a problem invalidating my timer.

@property (nonatomic, strong) NSTimer *timer;

Inside my block on success, I am allocating and setting my timer on main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:[Config refreshInterval].integerValue target:self selector:@selector(methodThatHasABlockCalledMentionedAbove) userInfo:nil repeats:NO];
});

At my textFieldDidBeginEditing I am invalidating my timer like this:

dispatch_async(dispatch_get_main_queue(), ^{
        [self.timer invalidate];
        self.timer = nil;
 });

My method still gets executed on timer. What's the catch?


回答1:


Maybe you are setting several timer objects without invalidating the old ones first?!

releasing a timer objects thats already scheduled will not invalidate the timer, so:

before you do self.timer = .... , call [self.timer invalidate];



来源:https://stackoverflow.com/questions/33128357/nstimer-does-not-invalidate

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