Open
Description
I am restricting the watchPosition to call setLocation only when 5 minutes has passed. This is needed to not overload the api with api calls every second. But this code does not work as expected. Can it be that the watchPosition can’t access the LOCATION_LAST_SYNCED_ON Stored variable?
Or is this not the correct way to achieve this?
const [syncedOn, setSyncedOn] = useMMKVStorage<string|undefined>('LOCATION_LAST_SYNCED_ON', storage, undefined);
const canSync = () => {
let willSync = false
if (syncedOn === undefined) {
willSync = true
}
if (syncedOn != undefined) {
console.log('syncedOn', moment(syncedOn))
console.log('miment', moment())
const difference = moment().diff(moment(syncedOn), 'seconds')
console.log('diff', difference)
if (difference > 5 * 60) {
willSync = true
}
}
return willSync
}
/**
* Watch Postition and update location
*/
useEffect(() => {
if (hasLocationPermission) {
Geolocation.watchPosition((position) => {
if (canSync()) {
dispatch(setLocation(position));
}
},
(error) => {
console.log('error geolocation', error.code, error.message);
crashlytics().log('error geolocation' + error.code + error.message);
}, {
enableHighAccuracy: true,
interval: 10000
})
}
}, [locationPermission, hasLocationPermission]);