首页 > 解决方案 > React Native - 如何从 Expo 中的 TaskManager 访问承诺值?

问题描述

当我进入地理围栏时,我正在测试后台定位任务,以与 Expo 的任务管理器进行本机反应。我已经掌握了基础知识,但我现在正在尝试访问从其中一项任务返回的承诺中的值,以便我可以显示您是否已进入或退出地理围栏。谁能帮我弄清楚如何正确访问我想要的值?

以下是与地理围栏相关的任务部分。我返回作为承诺的价值区域。我还没有弄清楚如何直接从任务管理器发送:

   // 3 Define geofencing task
    TaskManager.defineTask(TASK_CHECK_GEOFENCE, ({ data: { eventType, region }, error }) => {
      if (error) {
        // check `error.message` for more details.
        return;
      }
      if (eventType === Location.GeofencingEventType.Enter) {
        console.log("You've entered region:", region);
        //this.props.setEnterRegion({ inRegion: region })
        const final  = region
        return final
      } else if (eventType === Location.GeofencingEventType.Exit) {
        console.log("You've left region:", region);
        const final  = region
        return final
      }
    });

然后在 App.js 中,我在 componentDidMount 中调用该应用程序:

componentDidMount() {
  this.getLocationsPermissions();
  this.startBackgroundUpdate();
  this.startGeofence();

}

//define and start geofence
startGeofence = async () => {
  console.log('starting geofencing test ...')
  let x = Location.startGeofencingAsync('TASK_CHECK_GEOFENCE',
    [
    {
      identifier: 'court',
      latitude: this.state.myLocation.latitude,
      longitude: this.state.myLocation.longitude,
      radius: 20,
      notifyOnEnter: true,
      notifyOnExit: true,
     }
     ]
    )

this.sendTask(x)
  
};

当我尝试访问该值时,我可以看到承诺,但我不确定我哪里出错了。我更愿意直接从任务管理器发送到商店,但我遇到了麻烦:

//send to store from task
sendTask = (x) => {
  console.log('entered region...', x)
  
  this.props.setEnterRegion(x)

  
entered region... Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}

标签: react-nativeexpotaskmanager

解决方案


推荐阅读