React Native 任务管理器:任务“firstTask”已执行,但看起来未定义,确保在初始化阶段调用了“TaskManager.defineTask”

r3i60tvu  于 2023-05-29  发布在  React
关注(0)|答案(2)|浏览(175)

我正在运行一个EAS应用程序,我必须使用Expo-Task-Manager来处理后台位置。当我的应用程序构建时,我遇到了这个错误:

TaskManager: Task "firstTask" has been executed but looks like it is not defined. Please make 
sure that "TaskManager.defineTask" is called during initialization phase.

下面是我在应用程序中执行任务管理器的代码,但我很难找到在“初始化阶段”中调用它的位置。

import * as TaskManager from 'expo-task-manager'
import * as BackgroundFetch from 'expo-background-fetch'
import * as Location from 'expo-location'

const LOCATION_TASK_NAME = 'background-location-task'

useFocusEffect(
    React.useCallback(()=>{

       const requestBackgroundPermissions = async() =>{
       const {status} = await Location.requestBackgroundPermissionsAsync()
         if(status === 'granted'){
           await Location.startLocationUpdatesAsync('firstTask',{
             accuracy: Location.Accuracy.Balanced,
       });
     }
     requestBackgroundPermissions()

    },
    [],
   ),
 )

//在useFocusEffect之外

TaskManager.defineTask('firstTask',({data,errror})=>{
    if(error){
      alert('Something went wrong with background locations')
    }
    if(data){
      alert('Something went right with background locations')
      const{locations} = data
    }
})
7d7tgy0s

7d7tgy0s1#

因此,如果您遇到此问题,请尝试将任务管理器移动到App.js文件中。当应用程序加载时,任务管理器将是初始化阶段的一部分。如果您有任何问题,请随时联系,但它应该看起来像这样:

import React from "react"
import {StyleSheet} from 'react-native'
import * as TaskManager from 'expo-task-manager'
import * as BackgroundFetch from 'expo-background-fetch'
import * as Location from 'expo-location'

const LOCATION_TASK_NAME = 'background-location-task'

TaskManager.defineTask('firstTask',({data,errror})=>{
    if(error){
      alert('Something went wrong with background locations')
    }
    if(data){
      alert('Something went right with background locations')
      const{locations} = data
    }
})

export default function App(){

return <AppFile/>

}
xyhw6mcr

xyhw6mcr2#

我已经在我的index.js文件中使用了这个解决方案,它工作得很好

...
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import { store } from './src/store';
import { getNotificationCount } from './src/store/actions/NotificationsActions';

const FETCH_TASKNAME = 'BACKGROUND-NOTIFICATION-TASK';
const INTERVAL = 1;

export async function registerFetchTask() {
    TaskManager.defineTask(FETCH_TASKNAME, ({ data, error, executionInfo }) => {
        console.log('BACKGROUND-NOTIFICATION-TASK', data, error, executionInfo);
        if (store) {
            const profile = store.getState().myProfileSlice.profile;
            profile.id && store.dispatch(getNotificationCount(profile.id));
        }
    });

    const status = await BackgroundFetch.getStatusAsync();
    switch (status) {
        case BackgroundFetch.BackgroundFetchStatus.Restricted:
        case BackgroundFetch.BackgroundFetchStatus.Denied:
            console.log("Background execution is disabled");
            return;
        default: {
            console.log("Background execution allowed");

            let tasks = await TaskManager.getRegisteredTasksAsync();
            if (tasks.find(f => f.taskName === FETCH_TASKNAME) == null) {
                console.log("Registering task");
                await BackgroundFetch.registerTaskAsync(FETCH_TASKNAME);

                tasks = await TaskManager.getRegisteredTasksAsync();
                console.log("Registered tasks", tasks);
            } else {
                console.log(`Task ${FETCH_TASKNAME} already registered, skipping`);
            }

            await BackgroundFetch.setMinimumIntervalAsync(INTERVAL);
        }
    }
}

registerFetchTask();

registerRootComponent(App);

相关问题