Expo后台获取已初始化,但从未运行



在开发我的react原生应用程序时,我需要定期后台提取到另一台服务器。所以我从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();
}

现在,我尝试在安装组件时启动它(我使用类和typescript(:

class MainPage extends Component
{
componentDidMount() {
initBackgroundFetch();
}
.
.
.
}

但在我的控制台输出中;启用后台提取";从注册。。。我认为我的initBackgroundFetch放错了地方,但找不到我需要放的地方。

PS:我的expo诊断

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

根据测试后台获取的文档,您需要在MacOS上使用Instruments应用程序。

For iOS, you can use the Instruments app on macOS to manually trigger background fetches:
1) Open the Instruments app. The Instruments app can be searched through Spotlight (⌘ + Space) or opened from /Applications/Xcode.app/Contents/Applications/Instruments.app
2) Select Time Profiler
3) Select your device / simulator and pick the Expo Go app
4) Press the Record button in the top left corner
5) Navigate to the Document Menu and select Simulate Background Fetch - Expo Go:

我已经能够使用这些步骤成功地测试我的后台获取。

相关内容

最新更新