Skip to content

Commit 6013d52

Browse files
chore: fix watch clearing issues
1 parent 99f5636 commit 6013d52

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

ios/RNCGeolocation.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ - (void)enableBackgroundLocationUpdates
309309
RCT_REMAP_METHOD(startObserving, startObserving:(RNCGeolocationOptions)options)
310310
{
311311
checkLocationConfig();
312+
313+
if (_observingLocation) {
314+
[self stopObserving];
315+
}
312316

313317
// Select best options
314318
_observerOptions = options;
@@ -376,6 +380,12 @@ - (void)enableBackgroundLocationUpdates
376380
successBlock(@[_lastLocationEvent]);
377381
return;
378382
}
383+
384+
BOOL didPause = NO;
385+
if (_observingLocation) {
386+
[self stopObserving];
387+
didPause = YES;
388+
}
379389

380390
// Create request
381391
RNCGeolocationRequest *request = [RNCGeolocationRequest new];
@@ -400,6 +410,10 @@ - (void)enableBackgroundLocationUpdates
400410
[self beginLocationUpdatesWithDesiredAccuracy:accuracy
401411
distanceFilter:options.distanceFilter
402412
useSignificantChanges:options.useSignificantChanges];
413+
414+
if (didPause) {
415+
[self startObserving];
416+
}
403417
}
404418

405419
#pragma mark - CLLocationManagerDelegate

js/implementation.native.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import type {
2323

2424
const { RNCGeolocation, GeolocationEventEmitter } = GeolocationNativeInterface;
2525

26-
let subscriptions: (
27-
| [EmitterSubscription, EmitterSubscription | null]
28-
| undefined
29-
)[] = [];
26+
let subscriptions: {
27+
[key: number]: [EmitterSubscription, EmitterSubscription | null];
28+
} = {};
3029
let updatesEnabled = false;
3130

3231
/**
@@ -100,13 +99,13 @@ export function watchPosition(
10099
RNCGeolocation.startObserving(options);
101100
updatesEnabled = true;
102101
}
103-
const watchID = subscriptions.length;
104-
subscriptions.push([
102+
const watchID = Object.keys(subscriptions).length + 1000;
103+
subscriptions[watchID] = [
105104
GeolocationEventEmitter.addListener('geolocationDidChange', success),
106105
error
107106
? GeolocationEventEmitter.addListener('geolocationError', error)
108107
: null,
109-
]);
108+
];
110109
return watchID;
111110
}
112111

@@ -127,13 +126,9 @@ export function clearWatch(watchID: number) {
127126
// array element refinements not yet enabled in Flow
128127
const sub1 = sub[1];
129128
sub1 && sub1.remove();
130-
subscriptions[watchID] = undefined;
131-
let noWatchers = true;
132-
for (let ii = 0; ii < subscriptions.length; ii++) {
133-
if (subscriptions[ii]) {
134-
noWatchers = false; // still valid subscriptions
135-
}
136-
}
129+
130+
delete subscriptions[watchID];
131+
let noWatchers = Object.keys(subscriptions).length === 0;
137132
if (noWatchers) {
138133
stopObserving();
139134
}
@@ -148,16 +143,11 @@ export function stopObserving() {
148143
if (updatesEnabled) {
149144
RNCGeolocation.stopObserving();
150145
updatesEnabled = false;
151-
for (let ii = 0; ii < subscriptions.length; ii++) {
152-
const sub = subscriptions[ii];
153-
if (sub) {
154-
warning(false, 'Called stopObserving with existing subscriptions.');
155-
sub[0].remove();
156-
// array element refinements not yet enabled in Flow
157-
const sub1 = sub[1];
158-
sub1 && sub1.remove();
159-
}
160-
}
161-
subscriptions = [];
146+
Object.values(subscriptions).forEach(([sub, sub1]) => {
147+
warning(false, 'Called stopObserving with existing subscriptions.');
148+
sub.remove();
149+
sub1 && sub1.remove();
150+
});
151+
subscriptions = {};
162152
}
163153
}

0 commit comments

Comments
 (0)