transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

Understanding Lifecycle of JS Background Tasks #481

Closed pid0r-3blan closed 3 months ago

pid0r-3blan commented 3 months ago

I'm currently working with your library and have some questions regarding the lifecycle of JavaScript background tasks, particularly in scenarios involving background or suspended states (e.g., when background geolocation or similar method wakes up the app from suspended state).

My primary concern is about the behavior of the onEvent JS callback. Specifically, I'm curious if the code located in index.js of the application gets executed when the onEvent callback is triggered from a background or suspended state. In my index.js, I have essential initializations like service initialization, session restoration from async or encrypted storage, and registerAppLaunchedListener for navigation which includes further initialization, creation of an axios instance, etc.

My question is: If I require an initialized axios with a session in the onEvent callback, should I replicate all these initialization tasks within the onEvent function, or is it sufficient that the initialization is done inindex.js?

I understand this might depend on the platform (iOS/Android) and configuration (e.g., use of headless JS in Android), but I'm a bit unclear about how JS operates in such application states, and it's quite perplexing. Could you provide some insights on this, or recommend any resources for further reading?

christocracy commented 3 months ago

Are you testing fetch tasks by simulating events, as documented in the readme?

Why not try it and see?

pid0r-3blan commented 3 months ago

Thanks for the quick reply. I understand testing is important, but it's quite time-consuming to test every scenario on two devices (iOS + Android). A basic theoretical understanding would really help me start off on the right foot.

But you're right, better to know how this works on practice. Thanks for the advice!

christocracy commented 3 months ago

triggered from a background

The plug-in includes the word "background" in its name. This is on-purpose.