Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the time interval remaining from NSTimer

I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?

Thanks

like image 949
Henry Brown Avatar asked Oct 18 '15 13:10

Henry Brown


People also ask

How do you calculate remaining time in a timer?

We calculate the remaining time by the following steps: Subtract "Temporary Real" from 20 min and store as "Time Until Done" This seems like a lot of steps to calculate and display the remaining time in a timer.

What is the difference between nstimer and cfrunlooptimerref?

NSTimer is toll-free bridged with its Core Foundation counterpart, CFRunLoopTimerRef. See Toll-Free Bridging for more information. You specify whether a timer is repeating or nonrepeating at creation time. A nonrepeating timer fires once and then invalidates itself automatically, thereby preventing the timer from firing again.

How does a repeating timer schedule itself?

A repeating timer always schedules itself based on the scheduled firing time, as opposed to the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5-second time intervals, even if the actual firing time gets delayed.

What is a timer in C++?

A timer that fires after a certain time interval has elapsed, sending a specified message to a target object. Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.


2 Answers

You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.

The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.

E.G. something like:

let fireDate = yourTimer.fireDate
let nowDate = NSDate()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
like image 131
Michael Dautermann Avatar answered Nov 15 '22 07:11

Michael Dautermann


When using the Solution of @Michael Dautermann with normal Swift type Timer and Date I have noticed, that his solution will give you a negative value e.g.:

let fireDate = yourTimer.fireDate
let nowDate = Date()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
//above value is negative e.g. when the timers interval was 2 sec. and you 
//check it after 0.5 secs this was -1,5 sec.

When you insert a negative value in the Timer.scheduledTimer(timeInterval:,target:,selector:,userInfo:,repeats:) function it would lead to a timer set to 0.1 milliseconds as the Documentation of the Timer mentions it.

So in my case I had to use the negative value of the nowDate.timeIntervalSinceDate(fireDate) result: -nowDate.timeIntervalSinceDate(fireDate)

like image 28
Marcel T Avatar answered Nov 15 '22 06:11

Marcel T