ocetnik / react-native-background-timer

Emit event periodically (even when app is in the background)
MIT License
1.61k stars 227 forks source link

Background task creates the risk of termination #373

Open zoolle opened 2 years ago

zoolle commented 2 years ago

I have opened the app and on initial load I create a set interval to send the location to backend every 5 seconds. I left the app in foreground and then in Xcode logs started to show me this

2021-11-25 14:41:34.367850+0000 Driver[3314:522530] [BackgroundTask] Background Task 5 ("RNBackgroundTimer"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this. 2021-11-25 14:42:02.242027+0000 Driver[3314:523175] [tcp] tcp_output [C8.1:3] flags=[R.] seq=1461532209, ack=806701271, win=2047 state=CLOSED rcv_nxt=806701271, snd_una=1461532132 2021-11-25 14:44:59.632334+0000 Driver[3314:523177] [tcp] tcp_input [C18.1:3] flags=[R] seq=2256668869, ack=0, win=0 state=LAST_ACK rcv_nxt=2256668869, snd_una=1943136650 2021-11-25 14:44:59.632865+0000 Driver[3314:523177] [tcp] tcp_input [C18.1:3] flags=[R] seq=2256668869, ack=0, win=0 state=CLOSED rcv_nxt=2256668869, snd_una=1943136650 2021-11-25 14:44:59.633033+0000 Driver[3314:523177] [tcp] tcp_input [C18.1:3] flags=[R] seq=2256668869, ack=0, win=0 state=CLOSED rcv_nxt=2256668869, snd_una=1943136650

Any idea why I get this and how can I avoid it?

brunomartinezciompi commented 2 years ago

Same

guest2acute commented 2 years ago

Same issue.

Tim4497 commented 2 years ago

As far as I understand it you can just run code up to 30 seconds after the app enters background, and then you can only fire events again in a period of 15 minutes (also 30 seconds).

You could start the timer again with https://github.com/transistorsoft/react-native-background-fetch which runs code every 15 minutes, but also it could be less depending on the hidden iOS background algorithm which depends on how often a App is used by the user (less use, less frequent background runtime)

Please correct me when I'm wrong, still figuring this background stuff out with react-native.

dillonkelley commented 2 years ago

Has anyone faced this warning and seen timer performance issues with iOS in production? I'm getting timer issues with iOS but works seemingly fine on Android.

Irongade commented 2 years ago

@Tim4497 So does this mean we have to use this package in conjunction with react-native-background-fetch? Or should we solely use react native background fetch for background tasks?

Tim4497 commented 2 years ago

@Tim4497 So does this mean we have to use this package in conjunction with react-native-background-fetch? Or should we solely use react native background fetch for background tasks?

I used:

BackgroundFetch from 'expo-background-fetch'; TaskManager from 'expo-task-manager';

and

queue, { Worker } from 'react-native-job-queue';

in the end.

You can use them in bare rn projects, so no expo required