what is the best way to update a timer UI - ios

My app draws a timer (with detail to .1 seconds), for which I am currently using a NSTime which fires every .1 seconds. This feels like an absolutely terrible idea, but I'm not sure how else to do it. I don't really care about the .1 seconds updating always, but I would like it to update more than once per second. Is there a good way to do this?

NSTimer doesn't strike me as a bad approach. NSTimer is generally a very regular way to keep track of time (indeed it was used for animation timing before CADisplayLink came along). Unless you are seeing unacceptable performance of your timer display updating, I would stick with this approach.
If you are having issues with delays and inaccurate time readings, you could store the start time in NSDate, and continue to use the NSTimer but only to update the display. On each timer event firing, you then update the display by finding the NSTimeInterval from the start time to now. This way even if there is a performance issue, at least the time being display should remain accurate at the time of display.

Related

CMClock Swift Example

I have two iOS devices physically separate from each other. I need them to perform a task at exactly the top of every minute and bottom of every minute (so every 30 seconds) and stay synchronized.
My initial approach was to calculate the time until the top of the next minute and set a timer until that time and then start my 30 second timer.
The problem was that the 30 second timer would drift and eventually be out of sync.
I discovered an Apple API called CMClock. I could not find an example but was wondering if anyone has used this API and if so could provide an example of how to keep two devices synchronized like I described?

How to keep the NSTimer always work evenly

Now I am creating a metronome program. I use a NSTimer to bring the metronome into play. Of course The Timer works repeatedly. But I find out in two situation that the timer works not accruately.
When just start the NSTimer, the first two beat sometimes goes too closely. After then, the beat goes evenly.
When the app goes backgound I make the Timer work continuely by:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
but sometimes the unevenly beat also happens at the time when entering or coming back from the background status.
So I want to know how to keep the Timer always work evenly, no matter which situation it is in. Thks!
When setting up a timer firing at a certain time, the system implicitly sets an allowed "leeway" which is a short delay within the timer may actually fire.
This is due to "Timer Coalescing" - a system feature - which groups events happening roughly at the same time together and fire them at the exact same time. The reason is to reduce CPU cycles and extend the idle time of the CPU to save power.
If the timer interval is small (milli seconds to seconds) this "leeway" is in the range of 10% of the timer interval.
You can explicitly retrieve and set the current maximum allowed leeway value with the methods
-tolerance
-setTolerance:
See also:
NSTimer
Timing Accuracy

iOS is there a limit to duration of touchesbegan?

I'd like to do several things in my app after the user has touched - the longer they touch, the better - is there a limit? Will iOS 'force' a touchesEnded after too long?
There's likely no limit. The constraint it probably more a user experience one (at what point might a long press become annoying).
touchesEnded will be called when touch(es) end. You can (theoretically) take as much time as you want in touchesBegan handler. But that can (and probably will) result in bad UI responsiveness.
If you really need to do something that takes a lot of time you might want to consider starting a background thread in touchesBegan.
If memory serves me right touchesMoved will only be called when touchesBegan method reaches the end - touches will be buffered though.
EDIT: as for your updated question: no, there is no limit to this besides the battery and users patience. Depends on what you want to achieve there might be a more elegant solution than staying in touchesBegan.

How to observe MPMoviePlayerController according to the playback time?

In order to invoke some events for the specified time, for example, when the video goes to 10.0s, or 20.0s , there should be some events to be invoked, is that possible to observe some property of the instance of MPMoviePlayerController for such cases ? Or any other solutions ?
I had to do almost the same thing and I didn't find any good solution except by using my own NSTimer and look at some moments the current time of the MPMoviePlayer...
So basically, every seconds I'm watching what current time it is and when I'm interrested in the time, I do my stuff.

Timer and Dispatcher Timer in Emulator vs. Device

I need to use a timer in my WP7 application and display it's value in the UI.
I managed to get it working two ways: one using the DispatcherTimer class and the other using the Timer class.
The problem is that the DispatcherTimer is slower than "real-time" and the Timer class actually goes faster than "real-time". So both go wrong.
Is this because I'm running it in the emulator? Don't have a device to test it on. Anyone had this issue and tested on both?
To show time accurate to one second on screen, I use a DispatcherTimer that updates every 500 milliseconds and then subtract the current time from the start time to get the time elapsed and display that on-screen.
Does that help?

Resources