Skip to content

ACHP/rn-background-fetch-bug-repro

Repository files navigation

This is a new React Native project, bootstrapped using @react-native-community/cli.

Reproducing the bug

To reproduce the bug, simply run the app following react-native guide.

  • Then click on the bug blue "Register background fetch" button. (This will register a background task)
  • Then you can kill your app and run the background task with adb shell cmd jobscheduler run -f com.rnbgfetchbugrepro 999
    • You should see a logger in the metro console indicating the task started, stopped, and finished.
  • Then re-open the app and reclick the button
  • Re-kill your app and re-run the headless task with adb shell cmd jobscheduler run -f com.rnbgfetchbugrepro 999
    • You should now see an error in the logcat console [BGTask] failed to load config for taskId: react-native-background-fetch

Video of this process

rnbgfetch-repro.mp4

What is happening

From my research, I noticed that the second time we register the background task, it is not properly saved into the shared-preferences (We can see it in the video). This happens because on this line, the mConfig hashmap still contains the taskId when we relaunch the app the second time.

https://github.com/ACHP/transistor-background-fetch/blob/7ad81a8fca982c4e7a2abcce440fb2d36716b207/android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java#L88

And the hashmap still contains the taskId because when we call stop(), it clean the shared-preferences, but not the mConfig hashmap.

https://github.com/ACHP/transistor-background-fetch/blob/7ad81a8fca982c4e7a2abcce440fb2d36716b207/android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java#L146-L170

That is why we see a Re-configured existing task, instead of a fresh "configure". Then when we try to run the background task, it loads the config from the shared-preference, but it can't find nothing. So it fails with [BGTask] failed to load config for taskId: react-native-background-fetch

Suggestion

We should probablu clear the mConfig object when we stop the backgroundTask, so that there is no "desynchronisation" between the sharedPreference

Now, Running the app with React-Native

Note: Make sure you have completed the React Native - Environment Setup instructions till "Creating a new application" step, before proceeding.

Step 1: Start the Metro Server

First, you will need to start Metro, the JavaScript bundler that ships with React Native.

To start Metro, run the following command from the root of your React Native project:

# using npm
npm start

# OR using Yarn
yarn start

Step 2: Start your Application

Let Metro Bundler run in its own terminal. Open a new terminal from the root of your React Native project. Run the following command to start your Android or iOS app:

For Android

# using npm
npm run android

# OR using Yarn
yarn android

For iOS

# using npm
npm run ios

# OR using Yarn
yarn ios

If everything is set up correctly, you should see your new app running in your Android Emulator or iOS Simulator shortly provided you have set up your emulator/simulator correctly.

This is one way to run your app — you can also run it directly from within Android Studio and Xcode respectively.

Step 3: Modifying your App

Now that you have successfully run the app, let's modify it.

  1. Open App.tsx in your text editor of choice and edit some lines.

  2. For Android: Press the R key twice or select "Reload" from the Developer Menu (Ctrl + M (on Window and Linux) or Cmd ⌘ + M (on macOS)) to see your changes!

    For iOS: Hit Cmd ⌘ + R in your iOS Simulator to reload the app and see your changes!

Congratulations! 🎉

You've successfully run and modified your React Native App. 🥳

Now what?

Troubleshooting

If you can't get this to work, see the Troubleshooting page.

Learn More

To learn more about React Native, take a look at the following resources:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published