NSTimer不会无效 [英] NSTimer does not invalidate

查看:85
本文介绍了NSTimer不会无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使计时器失效时遇到问题.

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];
});

在我的textFieldDidBeginEditing处,我正在使计时器无效:

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?

推荐答案

也许您正在设置多个计时器对象而没有首先使旧对象无效?!

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:

在执行self.timer = ....之前,请先呼叫[self.timer invalidate];

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

这篇关于NSTimer不会无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆