Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

cocoa - NSTimer not firing when runloop is blocked

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

to calculate the elapsed time.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...