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

问题描述

我的应用程序刚刚完成,beta 测试在秒表部分发现了一个错误...秒表使用 nstimer 进行计数,并有一个表用于存储圈数,但是当圈数表滚动时,手表会停止或暂停并且不会弥补损失的时间.

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.

使用以下方法消除了拖延:

This was stalling was eliminated by using:

startingTime = [[NSDate date] timeIntervalSince1970];

计算经过的时间.

但我仍然使用 NSTimer 每 0.1 秒触发一次,这意味着即使经过的时间最终会正确更新,滚动仍然会使计时器停止……并将其与 Apple 秒表进行比较,这让我感到想知道那个秒表是否有一个单独的线程来计算经过的时间.有谁知道这是怎么做的吗?

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?

现在,使用自 Epoch 以来的时间在某种意义上运行良好,但它使启动、停止和& 的问题复杂化了.重新启动秒表

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 都触发需要将其添加到runloop的跟踪模式中.

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 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 08:01
查看更多