Running a background worker in React Native Expo for intensive tasks

Viewed 276

I would like to create a background-worker/-service that is running in the background that executes jobs that are queued. For example if a user is downloading a image, receiving a message or uplading files.

The idea would be like having a loop that waits for a semaphore to release:

class BackgroundWorker {

    async process() {
      while (true) {
        const job = await this.jobQueue.waitAsync(); // waits for a job being enqueued
        
        // process job...
      }
    }
}

I saw in the expo documentation that it is possible to use the TaskManager or the BackgroundFetch but these classes are not guaranteed to be executed in the interval that you can specify (https://docs.expo.dev/versions/latest/sdk/task-manager/ and https://docs.expo.dev/versions/latest/sdk/background-fetch/). I was using the BackgroundFetch and set the minimumInterval to 10 seconds. It was executing the function every minute. I need a solution where I can react directly to events.

Another question is, if it's possible to run these kind of workers in the background while the application is closed/terminated or running in the background.

0 Answers
Related