I need to fetch data from api and update in SQLite database every hour (Like backup and view when offline).
I have tried TaskManager with BackgroundFetch to run task at background, but it runs only when the app is minimised and not when the app is terminated.
Existing code
import * as BackgroundFetch from "expo-background-fetch";
import * as TaskManager from "expo-task-manager";
export async function registerTask() {
TaskManager.defineTask(TASKNAME,taskFunction);
const status = await BackgroundFetch.getStatusAsync();
switch (status) {
case BackgroundFetch.Status.Restricted:
case BackgroundFetch.Status.Denied:
console.log("Background execution is disabled");
return;
default: {
console.debug("Background execution allowed");
let tasks = await TaskManager.getRegisteredTasksAsync();
if (tasks.find((f) => f.taskName === TASKNAME) == null) {
console.log("Registering task");
await BackgroundFetch.registerTaskAsync(TASKNAME,{
minimumInterval: INTERVAL,
startOnBoot : true,
stopOnTerminate : false
});
tasks = await TaskManager.getRegisteredTasksAsync();
console.debug("Registered tasks", tasks);
} else {
console.log(`Task ${TASKNAME} already registered, skipping`);
}
// console.log("Setting interval to", INTERVAL);
// await BackgroundFetch.setMinimumIntervalAsync(INTERVAL);
}
}
}
In App.js
registerTask();
I really need something to work both on foreground and background and as well as when the app is closed.
Thanks in advance.
Note: Need solution compatible to Expo