Ways to implement a timer in iOS

What are some of the best / best ways to implement a countdown timer in iOS?

I did digging and tried to use NSTimers (1 s timer), but some people think that this is not a very good way, because it is not very accurate !!! Others claim that it will run out of battery, as it will always be in the background until the time runs out. I thought I could create several scheduled NSTimer so that it does not lose synchronization. Some suggest that I should write down the actual timestamp at the beginning and somehow use the built-in clock and use this to update the timer countdown .... except that I cannot figure out how to do this. Any ideas? How do you implement it?

+4
source share
2 answers

Every time you use a timer, the best option is always to record the start time. Then, whenever you update your interface, just make the difference between the current time and the start time. You can then use NSTimers to run interface updates at certain times.

+5
source

Indeed, NSTimer not very accurate. Thus, you usually should simply not multiply the time interval that you specified using the fire counter, but instead use some kind of timestamp (for example, a saved NSDate should work fine for your needs).

Regarding the discharge of the battery, I strongly doubt that a timer with an interval in the frequency domain of ~ 1 Hz has any measurable effect. And if I am not completely mistaken when the application enters the background, all timers will be paused and rescheduled from their respective fire date, when the application reappears in the foreground. - So no harm here, libor>

0
source

Source: https://habr.com/ru/post/1345257/


All Articles