transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

Update time is not as expected #472

Closed ifgabriel closed 6 months ago

ifgabriel commented 6 months ago

Your Environment:

Code

  const initBackground = async () => {
    const onEvent = async (taskId: string) => {
      if (taskId === 'UPDATE_COORDINATES') {
        console.log('[BackgroundFetch] task: ', taskId);
        BackgroundFetch.finish(taskId);
      };

    const onTimeout = async (taskId: string) => {
      console.warn('[BackgroundFetch] TIMEOUT task: ', taskId);
      BackgroundFetch.finish(taskId);
    };

    const status = await BackgroundFetch.configure(
      { minimumFetchInterval: .20, forceAlarmManager: true, requiredNetworkType: 0 },
      onEvent,
      onTimeout
    );

    console.log('[BackgroundFetch] configure status: ', status);
  };

  BackgroundFetch.scheduleTask({
    taskId: 'UPDATE_COORDINATES',
    forceAlarmManager: true,
    enableHeadless: true,
    delay: .20,
    periodic: true,
  });

  useEffect(() => {
    initBackground();
  }, []);

Expected Behavior

Should update coordinate every 20 seconds

Actual Behavior

It is updating the coordinates every 1 second

Steps to Reproduce

  1. npx react-native run-android
  2. access the screen that starts the background fetch
  3. exit the application

Context

I'm trying to update device coordinates even with the app running in the background

Debug logs

[Tue Oct 31 2023 11:18:50.901] LOG [BackgroundFetch] configure status: 2 [Tue Oct 31 2023 11:19:24.217] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Tue Oct 31 2023 11:20:42.110] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Tue Oct 31 2023 11:21:42.760] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Tue Oct 31 2023 11:22:42.760] LOG [BackgroundFetch] task: react-native-background-fetch [Tue Oct 31 2023 11:22:42.137] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Tue Oct 31 2023 11:23:42.760] LOG [BackgroundFetch] task: UPDATE_COORDINATES [Tue Oct 31 2023 11:24:42.740] LOG [BackgroundFetch] task: UPDATE_COORDINATES

christocracy commented 6 months ago

delay: .20,

Read the api docs. delay requires an integer and is measured in milliseconds

ifgabriel commented 6 months ago

@christocracy Even using 20000 in the delay, the task is executed every 1 minute, is this happening because the minimumFetchInterval is in minutes? it doesn't accept 0.20 correct? Do you know how I solve my problem?

christocracy commented 6 months ago

minimumFetchInterval is minutes.

You know that .scheduleTask is optional, right?

Simply calling .configure initiates a default periodic task of minimumFetchInterval minutes.

ifgabriel commented 6 months ago

Ok, but would I be able to do a periodic task of less than 1 minute?

christocracy commented 6 months ago

No.

ifgabriel commented 6 months ago

Is this an android or lib block?

christocracy commented 6 months ago

Android does not allow periodic AlarmManager tasks to run any less than every minute.

Attempting to fetch location every 20s using a periodic task is considered abusive to the user's device battery.

ifgabriel commented 6 months ago

Ok. Thank you for your time and explanations!