What is an NSTimer's behavior when the app is backgrounded?

前端 未结 2 1627
抹茶落季
抹茶落季 2021-01-18 13:07

I know that when you background an app, the timer stops running. However, what is the behavior when you come back from the background? Does the timer maintain its original <

2条回答
  •  生来不讨喜
    2021-01-18 13:55

    You can't use timers for this problem. Here is what I discovered after investigating a similar problem:

    • After an app has been in the background for about 30 seconds, the timer stops.
    • When you re-enter from background the timer starts to fire again.

    What you can do to overcome this problem and add this functionality properly you would have to implement the timers on your backend server. Hit a service when the timer starts and start the timer when the app is in foreground. The timer will take care of itself but when app is terminated or in the background the server will come into play. Hit a service to get how much time has elapsed or send a push notification when the 10 minutes have passed.

提交回复
热议问题