Help with removing Expo.Location.watchPositionAsync

Hello guys… I’ve been trying to implement Location.watchPositionAsync and it’s working fine… but I just can’t figure out how to remove the subscription, the code is as follows:

_checkIn = async () => {
    if (this.state.checked_in) {
      Alert.alert("YOU ARE ALREADY CHECKED IN!");
    } else {
      this.setState({ checked_in: true, });
      Alert.alert("SUCCESSFULLY CHECKED IN!");
      Expo.Location.watchPositionAsync({
        enableHighAccuracy: true,
        distanceInterval: 10,
      }, NewLocation => {
          let coords = NewLocation.coords;
          console.log("NEW LOCATION COORDS", coords);
     });
    }
  }

_checkOut = async () => {
    if (!this.state.checked_in) {
      Alert.alert("YOU ARE ALREADY CHECKED OUT!");
    } else {
      Expo.Location.watchPositionAsync().remove();
      this.setState({ checked_in: false, });
      Alert.alert("SUCCESSFULLY CHECKED OUT!");
    }
  }

Note: I tried different variations to remove the subscription… but none seems to work. What am I doing wrong?

Keep the return value of the original call to watchPositionAsync and store it somewhere and then call .remove() on that

Above, you’re setting up a new watch and immediately removing it but leaving the original one around.

1 Like

Thanks a lot @ccheever it seems I have forgotten to await the return value.

1 Like

Hi, Ahmad! Sorry if my message is offtopic in this topic, but can you show me please full work example’s code where you are using watchPositionAsync and it is working fine. Thank you for early and sorry for bad english :slight_smile:

1 Like