我正在运行一个EAS应用程序,在那里我必须使用博览-任务管理器来处理背景位置。当我的应用程序构建时,我会遇到这样的错误:
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
}
})发布于 2021-12-13 20:56:06
因此,如果遇到此问题,请尝试将任务管理器移动到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/>
}https://stackoverflow.com/questions/70336877
复制相似问题