0

I was able to successfully implement a countup timer using Timer.periodic().

If I switch between apps or take the app in the background for 30 seconds, current duration of the timer is still accurate.

However, if app is in the background for more than 2 minutes, current duration is not accurate and is actually delayed. Like the timer of the app slows down when it's in the background for too long.

Is using a background service a solution to this?

or should I just store the value of the duration when the app gets in the background and fetch it when it resumes?

Would like to get some suggestions. Thanks!

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
nerdy kid
  • 371
  • 1
  • 3
  • 14

1 Answers1

0

An operating system can stop your app in the background mode. You have to get a permission from the operating system for your app to continue working in a background mode. For example, you can set one of the background modes in Xcode:

Background modes in Xcode

But you need to think about your use case. You don't want your app to consume device memory and battery power in the background mode for no reason.

If you need to know how much time has passed, you can always save the last value before going into the background mode, and then compare it to the system time when the app wakes up.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58