开发者

Techniques for keeping iOS NSTimers from being killed in background

开发者 https://www.devze.com 2023-04-02 08:38 出处:网络
Relatively new to obj-c and iOS but I\'ve created a little app that is built around 4 simple, stopwatch-style timers. The user starts up a timer, it starts counting and they go on with their life. Whe

Relatively new to obj-c and iOS but I've created a little app that is built around 4 simple, stopwatch-style timers. The user starts up a timer, it starts counting and they go on with their life. When they open up the app they can see how long it's been going. Individually, these timers would be identical to the one in the Apple Clock app.

This works "most" of the time. I've had timers running for days, started, stopped, reset, started up again. What I've noticed though is that if the app is pushed just a little too far down the multitasking drawer, the next time I open up the app all my timers will be at zero, and the app will be in it's freshly launched state.

To me this appears related to the OS thinking my app is not needed, killing its threads/processes/whatever to free up memory. For 开发者_Python百科the apps intended audience, it will be a frequently checked app so this may not crop up as a problem, but it seems that there should be some technical approach to ensure that my stopwatches never fail. I'm just not sure where to look for this kind of functionality. Any thoughts appreciated.

Thanks!


You are doing it wrong.

If you build a stopwatch app with 10 timers you need exactly one NSTimer.
And this timer refreshes the display. This timer is not needed when the app is in background.

NSTimers have the problem that they can be late, and they should not be used to schedule time critical stuff (like counting a second up).


Store the current NSDate when you start a timer and display the difference in the app.

Store those NSDates to NSUserDefaults and they will even survive a restart of the device.


Try this approach: For your "timers" keep track of the start time only, and for display purposes calculate the number of seconds since the start time and display accordingly.

Store these start times so that even if the app is "evicted", you'll still know when a timer was started and can calculate how much time has elapsed.

The only reason to use an NSTimer would be if you want to show the timer's seconds ticking by.

Also have a look at the delegate methods applicationDidEnterBackground and applicationWillEnterForeground. In these methods you'd want to invalidate and re-establish an NSTimer (if you're using one), respectively.

There is a lot of Apple documentation about what you can and cannot do in the background here.


Unfortunately there is no way to ensure that your app will continue to run in the background the way you want it to.

You can use -beginBackgroundTaskWithExpirationHandler: to request up to ten minutes of additional execution time but in all likelihood your application will be terminated.

I've heard an Apple engineer refer to this as 'evicting'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号