当runloop被阻塞时,Nstimer不触发 [英] NSTimer not firing when runloop is blocked

查看:124
本文介绍了当runloop被阻塞时,Nstimer不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是关于完成我的应用程序和beta测试发现秒表部分中的错误...
秒表使用nstimer做计数,并有一个表存储圈,但当lap表

I am just about finished with my app and beta testing found a bug in the stopwatch portion... The stopwatch uses an nstimer to do the counting and has a table for storing laps, but when the lap table is scrolled the watch stops or pauses and does not make up for the lost time.

通过使用

startingTime = [[NSDate date] timeIntervalSince1970];

计算已用时间。

但我仍然使用NSTimer触发每0.1秒,这意味着滚动仍然停止计时器,即使已过去的时间将正确更新到最后...和比较这与苹果秒表它让我想知道是否秒表有一个单独的线程只为经过的时间计数。有人知道这是怎么做的吗?

but I am still using the NSTimer to trigger every 0.1 secs and that means that the scrolling still stalls the timer even though the elapsed time will be updated correctly in the end... and comparing this to the Apple stopwatch it makes me wonder if that stopwatch has a separate thread just for the elapsed time counting. Does anyone know if that is how it is done?

现在,使用从纪元开始工作的时间在某种意义上,但它使开始,停止的事情复杂化,&当手表停止时重新启动秒表

Now, using the time since the Epoch is working well in one sense, but it complicates the matter of starting, stopping, & restarting the stopwatch

,存储时间并用于计算手表重新启动时的偏移量,但似乎引入了一些延迟,

when the watch is stopped the time is stored and used to calculate an offset for when the watch is restarted, but there seems to be some latency introduced and the time jumps ahead visibly when the watch is restarted.

任何对根本原因或解决方案的想法都将非常感激。

Any thoughts toward the root cause or a solution would be greatly appreciated.

推荐答案

bbum的答案提供了一个更好的方式来设计你的应用程序,但如果你想要你的计时器触发,无论用户是否操纵UI或者你需要添加

bbum's answer provides a better way to design your application, but if you want your timer to fire regardless of whether the user is manipulating the UI or not you'll need to add it to the tracking mode of the runloop.

假设你正在为iPhone开发,那个模式是 UITrackingRunLoopMode 。如果您正在为Mac开发,还有一个类似命名的 NSEventTrackingRunLoopMode

Assuming that you're developing for the iPhone, that mode is UITrackingRunLoopMode. If you're developing for the Mac there is a similarly named NSEventTrackingRunLoopMode.

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

这篇关于当runloop被阻塞时,Nstimer不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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