Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update location once every 5 minutes not working as expected #425

Open
theobouwman opened this issue Nov 15, 2023 · 0 comments
Open
Labels
bug Something isn't working

Comments

@theobouwman
Copy link

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]);
@theobouwman theobouwman added the bug Something isn't working label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant