react-native-background-fetch: It only seems to work while debugging. It won't in release
Your Environment
- Plugin version: ^4.1.9
- Platform: iOS or Android: Android
- OS version: Android 11
- Device manufacturer / model: Oppo A9 2020
- React Native version (
react-native -v
): 0.70.6 - Plugin config:
Expected Behavior
It should run every 15 minutes on any android devices
Actual Behavior
Does not seem to work as i have a way to check when was last run and its always on default which is “pending…” instead of time of last run
Steps to Reproduce
- Install react native 0.70.6
- Maybe use same phone or similar android version
- Test check in release mode not debug mode
- See if runs and all ok
Context
I am trying to run a task every 15 minutes, a simple function will get called that has api to call the server for latest data, basically syncing users local db with remote db.
Debug logs
include iOS / Android logs
- iOS: XCode logs,
- Android: $ there is none as this is in release/production mode and no issues in debug mode
Code
import React, {
useContext,
createContext,
useCallback,
useState,
useEffect,
} from 'react';
import BackgroundFetch from 'react-native-background-fetch';
import {backgroundDataGetter} from './accountHelper';
const DailyTaskHookContext = createContext({});
export const DailyTaskContextProvider = ({children}) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => {
const taskCallback = async taskId => {
console.log('Background fetch started');
await backgroundDataGetter();
BackgroundFetch.finish(taskId);
console.log('Background fetch finished');
};
if (isAuthenticated) {
BackgroundFetch.configure(
{
minimumFetchInterval: 15, // 15 minutes
stopOnTerminate: false,
enableHeadless: true,
forceAlarmManager: true,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
},
taskCallback,
error => console.log('Background fetch failed to start', error),
);
BackgroundFetch.start();
console.log('Background fetch initiated');
} else {
BackgroundFetch.stop();
console.log('Background fetch stopped');
}
return () => {
BackgroundFetch.stop();
};
}, [isAuthenticated]);
const startDailyTask = useCallback(async () => {
if (__DEV__ !== true) {
BackgroundFetch.stop();
setIsAuthenticated(true);
}
}, []);
const stopDailyTask = useCallback(async () => {
setIsAuthenticated(false);
}, []);
return (
<DailyTaskHookContext.Provider
value={{
startDailyTask,
stopDailyTask,
}}>
{children}
</DailyTaskHookContext.Provider>
);
};
export const useDailyTaskHook = () => useContext(DailyTaskHookContext);
i got no idea what is wrong as no error or anything, am i missing something to add for release mode ? thanks.
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15 (4 by maintainers)
yes can, as long set well, i recommend just for calling apis and doing things like updating localdb, does its job well.