react-native-background-actions icon indicating copy to clipboard operation
react-native-background-actions copied to clipboard

BackgroundActions.stop does not terminate ongoing task

Open rakshitbharat opened this issue 2 years ago • 5 comments

stop not working

import BackgroundActions from 'react-native-background-actions';
import { DeviceEventEmitter } from 'react-native';

/**
 * Class BackgroundTaskManager is designed to manage background tasks using
 * react-native-background-actions. However, the task started by scheduleTask
 * does not stop when finish is called.
 */
class BackgroundTaskManager {
  static instance = null;
  static iteration = 0;
  static backgroundTaskId = null;

  constructor() {
    if (!BackgroundTaskManager.instance) {
      BackgroundTaskManager.instance = this;
      BackgroundTaskManager.resetInit();
      console.log('BackgroundTaskManager initialized');
    }

    return BackgroundTaskManager.instance;
  }

  /**
   * Resets the task manager and clears any existing background task configuration.
   */
  static resetInit() {
    this.configArgsiOS = null;
    this.configStartFunction = null;
    this.configTimeoutFunction = null;
    this.task = null;
    this.options = null;
    this.iteration++;
    console.log('BackgroundTaskManager reset');
  }

  /**
   * Configures the task manager with provided arguments.
   */
  static async configure(...args) {
    BackgroundTaskManager.resetInit();
    console.log('Configuring with args:', args);
    this.configArgsiOS = args;
    if (this.configStartFunction === null) {
      this.configStartFunction = args[1];
    }
    this.configTimeoutFunction = args[2];
    console.log('Configuration is now unified for both platforms.');
  }

  /**
   * Schedules a new background task. The task does not stop as expected when
   * the finish method is called.
   */
  static async scheduleTask(...args) {
    const { taskId } = args[0];
    console.log('Scheduling task with args:', args);

    // Task scheduling options
    this.options = {
      // ... existing options
    };

    // Task logic
    const veryIntensiveTask = async taskDataArguments => {
      // ... existing task logic
    };

    console.log(`Background task ${taskId} scheduled`);
    if (this.configStartFunction) {
      BackgroundActions.start(veryIntensiveTask, this.options);
    }
  }

  /**
   * Attempts to stop the background task. However, this does not
   * seem to stop the ongoing task.
   */
  static async finish() {
    BackgroundActions.stop();
    BackgroundTaskManager.resetInit();
    console.log(`Stopping background task`);
    DeviceEventEmitter.emit('stopBackgroundTask');
  }

  static delay = ms => new Promise(resolve => setTimeout(resolve, ms));
}

export default BackgroundTaskManager;

tried all ways but my internal hidden logic when i cann BackgroundActions.stop does not terminate ongoing task

rakshitbharat avatar Dec 05 '23 20:12 rakshitbharat

+1 I am also facing the same issue. Even after calling stop(), the service is not terminated.

Pavanbn03 avatar Jun 24 '24 09:06 Pavanbn03

Same here

Dwikavindra avatar Aug 24 '24 15:08 Dwikavindra

Same here. Stop doesn't seem to do anything except set a flag that it's stopped. I'm using this flag to terminate my backgrounded function but it's weird that stop doesn't seem to do anything to the process.

toppsdown avatar Jan 22 '25 11:01 toppsdown

Same here this needs to be fixed asap

collskin avatar Feb 16 '25 18:02 collskin

Same here, the stop of the backgroundTask is not stopping the previously started backgroundTask.

Florent75 avatar Oct 20 '25 11:10 Florent75