transistorsoft/react-native-background-fetch

Understanding Lifecycle of JS Background Tasks

Closed this issue · 3 comments

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 in index.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?

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

Why not try it and see?

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!

triggered from a background

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