react-native-geolocation-service icon indicating copy to clipboard operation
react-native-geolocation-service copied to clipboard

Unable to disable position observation in background on iOS (clearWatch is not working properly)

Open alessandro-bottamedi opened this issue 3 years ago • 3 comments

Hi, on iOS 14 I'm listening to the appstate to stop location if the app goes in the background. But it seems that with version 5.1.1 watch location remains active despite calling clearWatch (the blue location indicator is on in the background). If I call stopObserving the indicator disappears but I get the warning (Called stopObserving with existing subscriptions). With version 5.0.0 everything worked correctly.

Here my code:

  const watchId = useRef(null);

  // Effetto per watch position con app attiva
  useEffect(() => {
    if (appstate === 'active') {
      log.debug('GEOLOCATION WATCH: start watch');
      watchId.current = Geolocation.watchPosition(
        position => {
          log.info(position)
        },
        error => {
          if (error) {
            log.warn('GEOLOCATION WATCH: ' + error.code + ' | ' + error.message);
          }
        },
        {
          enableHighAccuracy: true,
          distanceFilter: 10,
          interval: 10000,
          fastestInterval: 5000,
          showLocationDialog: false,
          forceRequestLocation: false,
        }
      );
    } else {
      if (watchId.current) {
        log.debug('GEOLOCATION WATCH: stop watch');
        Geolocation.clearWatch(watchId.current);
        watchId.current = null;
      }
    }
    return () => {
      if (watchId.current) {
        log.debug('GEOLOCATION WATCH: stop watch');
        Geolocation.clearWatch(watchId.current);
        watchId.current = null;
      }
    };
  }, [appstate]);

What i'm doing wrong?

alessandro-bottamedi avatar Dec 22 '20 17:12 alessandro-bottamedi

There's no subscription related changes in 5.1.1, just a new property was added for accuracy control. It's probably some kind of bug in app side as far as I can tell. The code you provided looks ok, so please check the other parts of the code.

Agontuk avatar Dec 23 '20 13:12 Agontuk

Hi @alessandro-bottamedi ,

Had a similar issue, the problem for me was that watchId.current can be equal to 0 and thus would be falsy

Almouro avatar Apr 06 '21 12:04 Almouro

Try this....

console.log('stop')

try { store.dispatch(setMountTimer(false)) // Geolocation.clearWatch(store.getState().app.watchId); Geolocation.stopObserving() showToast('Navigation was stopped','success') } catch (error) { console.log(error) }

peterchijioke avatar Mar 25 '24 14:03 peterchijioke