NSTimer不会因无效而停止 [英] NSTimer doesn't stop with invalidate

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

问题描述

我添加这样的计时器

tim=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTim) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:tim forMode:NSDefaultRunLoopMode];

tim 这是我班级的NSTimer属性。

tim it is NSTimer property of my class.

然后我按下按钮点击它就像

Then i stop it on button click like

[[fbt tim] invalidate];
[fbt setTim:nil];

fbt它是我班级的实例。

fbt it is instance of my class.

如果我只调用invalidate然后它不会停止,但是如果我将它设置为nil然后我得到了EXC_BREAKPOINT

if i call only invalidate then it doesn't stop, but if i set it to nil then i got EXC_BREAKPOINT

这里的选择器中的repeatTim方法代码

here code of repeatTim method in selector

AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate.wbv stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"intal()"]];

我尝试拨打 init 无效 in

dispatch_async(dispatch_get_main_queue(), ^{})

它也不会停止计时器。

推荐答案

阅读文档NSTimer:

Read documentation for NSTimer:


创建计时器的方法有三种:

There are three ways to create a timer:


  1. 使用scheduledTimerWithTimeInterval:invocation:repeats:或scheduledTimerWithTimeInterval:target:selector:userInfo:repeat:class方法创建计时器并在默认模式下在当前运行循环上安排它。

  1. Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

使用timerWithTimeInterval:invocation:repeats:或timerWithTimeInterval:target:selector:userInfo:repeats:class方法创建计时器对象,而不在运行循环上调度它。 (创建它之后,必须通过调用相应NSRunLoop对象的addTimer:forMode:方法手动将计时器添加到运行循环中。)

Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

分配timer并使用initWithFireDate初始化它:interval:target:selector:userInfo:repeats:方法。 (创建它之后,必须通过调用相应NSRunLoop对象的addTimer:forMode:方法手动将计时器添加到运行循环中。)

Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)


您正在使用的方法已将其从1添加到mainLoop。 - 您需要删除此行或使用2.方法创建计时器并保留手动添加。

You are using method which already adds it to mainLoop from 1. - you need to remove this line or create a timer with 2. approach and leave manual adding.

还要记住,必须从安装了计时器的线程发送无效消息。如果从另一个线程发送此消息,则可能无法从其运行循环中删除与计时器关联的输入源,这可能会阻止该线程正常退出。

Also remember that you must send invalidate message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

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

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