CMClock Swift Example - ios

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?

Related

How to simulate a long time spent in background state for my iOS app?

I'm having issues with my app not properly restoring its previous state after it has spent some time in the background ( I'd say around 15 minutes ).
To work on that bug, I usually compile my code, put my app in background and wait around 15 minutes. Then I select my app again and I can use it with the debugger activated.
That's not efficient at all, so I was wondering if there was a way to simulate the fact that an app spends some time in the background.
Thanks!

iOS 7-8 Background Timer

I've searched a lot and didn't find anything that helps. Basically I need to develop an app, that vibrates every X minutes in time interval (provided by user, from 15 to 180 min), also it must be executed in background. Can this be achieved?
UPD:
It should play sound, vibrate or both of these. For example: user selects 40 mins, and sound should be played every 3 minutes. It must play/vibrate in background and foreground, after time passes, notifications should simply stop.

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

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?

what is the best way to update a timer UI

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.

Resources