NSTimer and Inactive/Sleep state - iphone

I have found the answer for this in iPhone: Detecting user inactivity/idle time since last screen touch but what happens when the device gets into inactive/sleep state? The timer stops firing right?

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
So, when your app will go to an inactive state - the timer will stop firing. And when your app will go back to an active state - the timer will resume firing again.

Related

Flutter - Timer pauses unintentionally

I have implemented a Timer as detailed in the article below, so that the timer will keep running when i change tabs.
How to implement persistent stopwatch in Flutter?
I have an issue where the Timer "loses" or pauses time, only when not plugged into my computer. When i plug it into my computer, no issues, the emulator runs fine too. I test the flutter timer against a timer on my computer or phone, and after 5mins, they match. But when my phone (Galaxy S9) isn't plugged in, after 5mins of real time, the flutter timer might only be up to 2mins, when i go back into the app the timer is running but the timer wont be what it should be. It doesn't crash or reset, its counting when i get back into the app. I'm finding it hard to debug as when I plug the phone into my computer it seems to work fine! Any ideas?
I had issues with this as well. Things working correctly when running on the Simulator is fools gold. The Timer won't run in the background like you want it to unless you do specific work to keep it running. Here is the official page from Flutter on doing work in the background: https://flutter.dev/docs/development/packages-and-plugins/background-processes
I'll suggest implementing didChangeAppLifecycleState method of the WidgetsBindingObserver and detect when the app goes to the background and when it's back in the foreground. At the point where the app goes into the background, you save the current time e.g (onPauseTime) (you can use SharedPreferences here) and the Duration left on the timer (timerDurationLeft).
When the app is back in the foreground you check if the timer was running before the app entered the background state, if it was, you then check the current time e.g (onResumeTime) and calculate the time difference in seconds (onResumeTime - onPauseTime), using the time difference and the timerDurationLeft you can calculate how much time the timer has left to run and you start/resume the timer from that point or end/set the timer to 0:00 if time has already passed.
P:S Using a background service would be the way to go if you intend to perform some actions in the middle, like set notifications at some point where the timer is almost complete.

Is there an alternative to using an NSTimer to display remaining time of audio file with AVAudioPlayer?

I play music with AVAudioPlayer and want to display the remaining time. It seems many suggest an NSTimer that fires every second but I am afraid this is inefficient and prone to error. Since NSTimer is not very precise timing (I have heard) it can be that the timer fires twice within the same second of audio, and the countdown then jumps over one second. Also this is a pull model.
Is there a more savvy way where AVAudioPlayer would call my countdown every time a second ticks away?
While NSTimer is semi-inaccurate, you can still just have it call every second or so and get information from the currentTime property of the song, and compare it to the duration property. From that you can calculate remaining time etc.
AVAudioPlayer Documentation
There are no callbacks available to help you handle updating the time display when playing an audio file. The NSTimer method is how it should be done. If you're concerned with inaccuracies in the display due to NSTimer firing precision, just reduce your timer's update interval. Keep the workload light in your implementation and set the interval to 500ms, or perhaps 250ms...

Is a Timer disabled automatically when app resigns active on the iphone?

Playing around with Timers, and trying to make them behave right when app is sent to background, killed etc.
Judging by the generated comments in xcode, you should disable/invalidate timers on resignActive/enterBackground events.
However, i havent done that yet, but checked what happens to my timer when i click the home button and then re-enter the app.
I basically have a method in my view that the timer triggers every second:
NSLog(#"workedTimTimerTick workedTime; %#", timeString);
And when i exit the app, the output stops, when i re-enter the app, the output starts again...
Since i'm not doing anything code-wise to the timer on those lifecycle events, how come it stops getting called?
Input appreciated!
Your app is suspended when it enters background mode (Application States and Transitions).
Your timer won't fire when the app is in background and the time spent in background isn't taken into account for the timer delay.

iPhone - NSTimer - Peculiar task needed

I know how to set a timer and when timer expires, it fires and a action is caught.
What I'm expecting is that after 30 seconds of every one hour of real time that that "xx:00:30" I have to check my server for updates.
I know how to communicate with server. But how can I create such a timer that fires every "xx:00:30".
I don't want to run the timer if the app is in background.
Any ideas on how to do this?
As mentioned by Marcus in his last message,
NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats: or setFireDate:
works.

Is it possible to run a task periodically in Background - iOS 4

I want to poll the location from GPS in the background. So we used a NSTimer and performed periodic checks in the timer tick. In other words, can we schedule NSTimer while the app is in Background?
Thanks in advance
You can't periodically execute arbitrary code, but you can get pinged on significant location changes via startMonitoringSignificantLocationChanges in CLLocationManager.