question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to run multiple instances of background-fetch?

See original GitHub issue

Your Environment

  • Plugin version:2.7.0
  • Platform: iOS and Android
  • OS version:
  • Device manufacturer / model:
  • React Native version (react-native -v):0.60.3
  • Plugin config

Expected Behavior

Initiating background-fetch in deferent parts of app with deferent callbacks, without overwriting any background-fetch previous process

Actual Behavior

Last background-fetch Initiation overrides the previous one

Steps to Reproduce

1.put code to intiate background-fetch in a separate class
2. instantiate background-fetch class in other components with a callback provided as an argument

background- fetch class:

import BackgroundFetch from "react-native-background-fetch";


export default class BackgroundFetch {

  constructor(fetchFunction){
    this.fetchFunction = fetchFunction;
  }


  initBackgroundFetch() {
    // Configure it.
    BackgroundFetch.configure({
      minimumFetchInterval: 15,     // <-- minutes (15 is minimum allowed)
      // Android options
      stopOnTerminate: false,
      startOnBoot: true,
      requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
      requiresCharging: false,      // Default
      requiresDeviceIdle: false,    // Default
      requiresBatteryNotLow: false, // Default
      requiresStorageNotLow: false  // Default
    }, () => {
      console.log("[js] Received background-fetch event");
      this.fetchFunction();
      // Required: Signal completion of your task to native code
      // If you fail to do this, the OS can terminate your app
      // or assign battery-blame for consuming too much background-time
      BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
    }, (error) => {
      console.log("[js] RNBackgroundFetch failed to start");
    });

    // Optional: Query the authorization status.
    BackgroundFetch.status((status) => {
      switch(status) {
        case BackgroundFetch.STATUS_RESTRICTED:
          console.log("BackgroundFetch restricted");
          break;
        case BackgroundFetch.STATUS_DENIED:
          console.log("BackgroundFetch denied");
          break;
        case BackgroundFetch.STATUS_AVAILABLE:
          console.log("BackgroundFetch is enabled");
          break;
      }
    });
  }
}

instantiating in other components:

async componentDidMount() {
    await this.checkInternet();
    const backGroundFetchHome = new BackgroundFetchClass(this.checkInternet.bind(this))
    backGroundFetchHome.initBackgroundFetch();

  }
async componentDidMount() {
    this.main();
    const backGroundFetchChat = new BackgroundFetchClass(this.main.bind(this))
    backGroundFetchChat.initBackgroundFetch();
 
  }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christocracycommented, Oct 3, 2019

Can you please elaborate more on this.

Think of a background-task as a balloon. Each background-fetch callback is a pin poking the balloon when #finish is called. The first pin that pops the balloon terminates the task. If each task were executing an HTTP request, one request may not complete.

0reactions
medmo7commented, Oct 3, 2019

thanks i will close this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using background tasks to update your app - Apple Developer
Open the project editor and select the desired target. · Click Signing & Capabilities. · Expand the Background Modes section. · If you're...
Read more >
Fetching multiple instances of LPLinkMetadata as app goes ...
I have to implement this task as the app goes into the background in order to preserve the user experience, so I have...
Read more >
Introducing Background Fetch - Chrome Developers
Background fetch lets you handle large downloads, even if the browser closes.
Read more >
iOS background processing - Background App Refresh Task
To simulate background fetch, from the tab bar > Debug > Simulate background fetch. Note that it works only when running on real...
Read more >
Background Modes Tutorial: Getting Started
Background Fetch : Perform background updates on a schedule determined by iOS. ... Background processing: Perform longer critical processes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found