How to run multiple instances of background-fetch?
See original GitHub issueYour 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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.thanks i will close this issue