【问题标题】:Expo background fetch initialize but never running世博会后台获取初始化但从未运行
【发布时间】: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


    【解决方案1】:

    根据 documentation 测试后台获取,您需要在 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:
    

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

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多