Skip to content

[Bug]: watchPosition subscription.remove() causes error and cannot be stopped without app termination #2573

@jpauldan

Description

@jpauldan

Required Reading

  • Confirmed

Plugin Version

5.1.1

Mobile operating-system(s)

  • iOS
  • Android

Device Manufacturer(s) and Model(s)

Google Pixel 7

Device operating-systems(s)

Android 16

React Native / Expo version

0.81.5 / 54.0.33

What happened?

The response to a call to watchPosition is supposedly a Subscription instance. However when calling remove() on that I get an error. I am unable to stop watching using the current documentation procedures.

TypeError: watchSub.remove is not a function (it is undefined)

What's a bit odd is that I log the contents of my Subscription instance and it appears to to have a remove function. But no matter what I get this error when calling it.

{"_h": 0, "_i": 3, "_j": {"_h": 0, "_i": 1, "_j": {"id": -1, "remove": [Function remove]}, "_k": null}, "_k": null}

I realize that you strongly discourage using watchPosition which is fine. We use it in rare circumstances for troubleshooting when a specific device doesn't behave as expected and stops updating even though the user reports they kept moving.

Also I tried to find an online code sample site that supports Expo with dev builds (because I'm pretty sure that Expo Go doesn't work with this plugin) but I couldn't find anything that allowed me to give you a working repro example. Let me know if there's an option I missed.

Plugin Code and/or Config

// Config / ready call.

BackgroundGeolocation.ready({
	geolocation: {
	  desiredAccuracy: BackgroundGeolocation.DesiredAccuracy.High,
	  distanceFilter: 3,
	  disableElasticity: true,
	  stationaryRadius: 3,
	  showsBackgroundLocationIndicator: true, 
	},
	activity: {
	  disableStopDetection: true,
	},
	app: {
	  backgroundPermissionRationale: {
		title: "Allow **** to access this device's location in the background?",
		message: 'In order to record your location in the background while running tracks please enable "{backgroundPermissionOptionLabel}" location permission',
		positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
		negativeAction: "Cancel"
	  },
	},
	logger: {
	  debug: true,
	  logLevel: BackgroundGeolocation.LogLevel.Verbose,
	});




// Repro code:

const sleep = (ms: number) => {
  return new Promise(resolve => setTimeout(resolve, ms));
}

const watchSub = BackgroundGeolocation.watchPosition({
  interval: 3000
}, (location) => {
  console.log('TIMED LOCATION');
}, (errorCode) => {
  console.log(errorCode);
});

await sleep(10000);
console.log('watchSub', watchSub);
watchSub.remove();

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions