NSTImer事件会阻塞主线程吗? [英] Would NSTImer events block the main thread?

查看:263
本文介绍了NSTImer事件会阻塞主线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用NSTimer时,一旦在上述间隔后调用回调,UI是否会被阻止?

When we use a NSTimer, once the call back is called after the mentioned interval, does the UI would be blocked?

推荐答案

来自文件


定时器与run
循环一起使用。要有效地使用计时器,
应该知道运行循环
如何操作 - 参见NSRunLoop和Threading
编程指南。

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide.

一旦在运行循环上调度,
计时器将以指定的间隔
触发,直到它失效。
非重复计时器在触发后立即使
无效。但是,重复计时器
,你必须通过调用invalidate方法
自己使计时器对象
无效。
调用此方法请求从当前
运行循环中删除
计时器;因此,你应该
总是从
中调用invalidate方法,该方法是安装了
的计时器的同一个线程。使计时器
无效会立即禁用它,这样它就不会使
更长时间影响运行循环。运行
循环然后删除并释放
计时器,就在
invalidate方法返回之前或在某个
之后的点。一旦无效,计时器
对象就无法重复使用。

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

我相信 NSTimer 不会阻止UI线程。另请查看以前的SO问题 NSTimer和更新UI

I believe NSTimer does not block the UI thread. Also take a look at this previous SO question NSTimer and updating UI

这篇关于NSTImer事件会阻塞主线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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