capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
177 stars 54 forks source link

Help to stop watcher on typescript #76

Closed 3rikson closed 1 year ago

3rikson commented 1 year ago

Hello guys

I'm having an issue to stop the watcher when a condition is satisfied. I send the location to an API, and when a variable is set in the return of this API, the watcher should be removed, but it is not working for me, it keeps sending the data to the API.

This is what I'm doing as service:

import {BackgroundGeolocationPlugin} from '@capacitor-community/background-geolocation'; const BackgroundGeolocation = registerPlugin('BackgroundGeolocation');

watcherId;

startTracking(){
   this.watcherId = BackgroundGeolocation.addWatcher({...}
   sendDataAPI(location);
   );
}

async sendDataAPI(location){
   let return = await http.post...
   if(return.data.var != null)
   {
      BackgroundGeolocation.removeWatcher({
         id: this.watcherId
      });
   }
}

Even the variable coming as expected, the command to stop the watcher doesn't works. I read in previous posts that async may be an issue, but the typescript forces you to insert it to wait the promisse from the API. I tried different approaches, but without success to stop the watcher. Have you guys faced it?

Thank you in advance

Erikson

3rikson commented 1 year ago

Hello

I had help of a friend and we solved the issue, my error was regarding the promisse that I was considering as watcher, and not the id of it.

startTracking(){
   BackgroundGeolocation.addWatcher({...}
   sendDataAPI(location);
   )).then(id => {
        this.watcherId = id;
    });
}

Hope it helps who finds him/herself in the same situation. Best regards guys