capacitor-community / background-geolocation

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

Unable to removeWatcher, missing ID. #11

Closed gbrits closed 3 years ago

gbrits commented 3 years ago

This is the error I'm receiving in my Logcat: Line 1 - Msg: Unhandled Promise rejection: Missing id. ; Zone: <root> ; Task: null ;

I'm having trouble removing the watcher on Android (Pixel 4) - I am not using the const id = approach, because I start at one method & end at another method.

Variable declaration at the class level:

clockedOnId: any;

Then in my start method:

this.clockedOnId = BackgroundGeolocation.addWatcher({...

I have attempted several approaches:

Plugins.BackgroundGeolocation.removeWatcher(`${that.clockedOnId}`);
// Plugins.BackgroundGeolocation.removeWatcher(that.clockedOnId);
// BackgroundGeolocation.removeWatcher(that.clockedOnId);

None of which work on Android. On Apple all appears to be well, but it's harder to tell without the notification sitting in the notification tray. Closing the app seems to be the only desired result.

Any assistance will be appreciated.

diachedelic commented 3 years ago

removeWatcher expects an object containing the ID, not the ID itself. Try this:

Plugins.BackgroundGeolocation.removeWatcher({
    id: that.clockedOnId
});
gbrits commented 3 years ago

Thanks for that @diachedelic - apologies just one final question; I have my http call to my restful api in the location spot of the callback, it appears to rapidfire gps inserts when I foreground the app on Android? Do you have a recommendation on where to place your http method of saving the location to avoid this?

diachedelic commented 3 years ago

You could either use a debounced function, or make use of the distance filter option.

gbrits commented 3 years ago

So I have it set to 50 (distanceFilter) and it appears to work well initially, but after about 10 minutes it appears to start getting stuck, then it appears to send the GPS points rapidly once I foreground the app - causing throttling / same created_at stamps on my backend, which then throws off my sequencing. I'm so close now, if I can overcome this hurdle it's perfect 😔

diachedelic commented 3 years ago

I'm sorry, I can't help you debug that. If you discover what is going on please let me know.

gbrits commented 3 years ago

I guess my concise question is: Is this unusual? Are our http calls meant to exist within the callback function?

diachedelic commented 3 years ago

The body of the callback function is the only place you can get location information. What you do with that information is up to you. If ordering is important to you, you might want to look at using WebSockets instead of seperate HTTP requests per location.