Skip to content

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

Open
@theobouwman

Description

@theobouwman

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]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions