【发布时间】:2022-08-18 05:15:28
【问题描述】:
在开发我的 react native 应用程序时,我需要定期后台获取到另一台服务器。 所以我从 expo 导入 2 类:
import * as BackgroundFetch from \'expo-background-fetch\';
import * as TaskManager from \'expo-task-manager\';
并初始化我的后台任务:
const fetchFunc = async () => {
try{
console.log(\"Hi from fetch function !\")
return BackgroundFetch.BackgroundFetchResult.NoData;
}
catch(err)
{
return BackgroundFetch.BackgroundFetchResult.Failed;
}
}
注册此任务:
async function registerBackgroundFetchAsync() {
try{
await BackgroundFetch.registerTaskAsync(\"func-fetch\", {
minimumInterval: 5, // 5 second
})
console.log(\"background fetch enabled\")
}
catch(err){
console.error(err);
}
}
并创建一个函数来执行所有这些:
async function initBackgroundFetch() {
if(!TaskManager.isTaskDefined(\"func-fetch\")){
TaskManager.defineTask(\"func-fetch\", fetchFunc)
}
await registerBackgroundFetchAsync();
}
现在,我尝试在安装组件时启动它(我使用类和打字稿):
class MainPage extends Component
{
componentDidMount() {
initBackgroundFetch();
}
.
.
.
}
但是在我的控制台输出中,我刚刚从注册中获得了“启用背景提取”... 我认为我的 initBackgroundFetch 放错了地方,但找不到我需要放的地方。
PS:我的博览会诊断
Expo CLI 5.0.3 environment info:
System:
OS: Linux 5.15 Kali GNU/Linux Rolling 2021.4
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
npm: 8.1.0 - ~/.nvm/versions/node/v14.16.1/bin/npm
npmPackages:
expo: ^43.0.3 => 43.0.3
react: 17.0.1 => 17.0.1
react-dom: 17.0.1 => 17.0.1
react-native: 0.64.3 => 0.64.3
react-native-web: 0.17.1 => 0.17.1
npmGlobalPackages:
expo-cli: 5.0.3
Expo Workflow: managed
标签: javascript reactjs typescript react-native expo