react-native-background-job icon indicating copy to clipboard operation
react-native-background-job copied to clipboard

Kill jobs when app gets closed

Open dev-andremonteiro opened this issue 5 years ago • 4 comments

I've been trying to cancelAll jobs after the user closes the app and I don't really see an easy way to do this. My current approach is relaying in the fact that every time I close the app the variables inside the main javascript environment gets reseted.

// Make this variable true inside your main component, this way when the job 
// gets executed we know the user have opened the app.
let control = false

function myJob(){
  // This variable will be false when the job gets called and the app have been
  // closed since the variable resets it's value.
  if(!control) { 
    CANCELALL()
  }
  //
  // Do what you want here.
}

Do you have an example on how to do this easier ? Maybe it should be an option in schedule?  Thank you for this amazing library.

dev-andremonteiro avatar Aug 28 '19 02:08 dev-andremonteiro

Hi @dev-andremonteiro This library is used to schedule background jobs even if the user closes the app. I didn't understand your usage .

Have a look at the app states here : https://facebook.github.io/react-native/docs/appstate

rasheedk avatar Oct 17 '19 08:10 rasheedk

I was trying to make this run only while the app was on the background and once the user closes it, it stops working. That's my use case.

dev-andremonteiro avatar Oct 18 '19 20:10 dev-andremonteiro

use this library instead -> https://github.com/ocetnik/react-native-background-timer

vbashiri avatar Dec 07 '19 08:12 vbashiri

i think i must use this library too, it does not work and no error tip,

yexiang1994 avatar Dec 26 '19 09:12 yexiang1994