setInterval not working in iOS when app in background

I have a requirement to call an API after several minutes as per setting,
I have already added the ionic Cordova background mode plugin

what is happening is:

  • when the timer is on (using interval and subscriber) and the app goes in the background, the timer works at half speed, i.e. interval is called every second but in 1 minute it is getting executed only 30 times.
  • After 10 minutes app gets suspended from the background and all the data gets lost.

can you please help me with how we can manage this using ionic 4 in iOS?

You shouldn’t use a setInterval or setTimeout when running in the background. There is no guarantee your app and web code will be given time by the OS to execute background tasks.

Also, keep in mind the cordova background mode plugin has this big warning:

Instead, a better approach would be to move to not needing this feature or building whatever you need natively to do a periodic fetch/etc. That, or find specialized plugins like this one for background geolocation:

It’s not ideal but it’s important to realize that iOS in particular and Android treat background processes very tightly and so you have to bend to their will to build proper background apps.

1 Like