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

BackgroundService.start create multiple task #170

Closed
Allan-Tecchio opened this issue Feb 2, 2023 · 5 comments
Closed

BackgroundService.start create multiple task #170

Allan-Tecchio opened this issue Feb 2, 2023 · 5 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@Allan-Tecchio
Copy link

When I kill my application and open it again and run BackgroundService.start it creates 2 tasks, even running BackgroundService.stop

@BillDelvin
Copy link

Hi.. I'm face the same issue, is there any way to solve this problem ?

@alexfov
Copy link

alexfov commented Mar 22, 2023

Duplicate
#121 here is the original issue. It's not solved yet.

@alexfov
Copy link

alexfov commented Mar 22, 2023

I made a crunch to solve the problem without touching the native part.

//this variable is persistent until the app is reloaded with service off
//so we check in a task if the fn argument is the same as 'iteration'
//if it is not we should stop backgroundTask
export let iteration = 0;

export const startBackgroundService = (isThereActiveShift: boolean) => {
  if (!BackgroundService.isRunning()) {
    iteration++;
    BackgroundService.start(backgroundTask.bind(null, iteration), taskOptions)
  } 
};

export const backgroundTask = async (index: number, taskDataArguments: { delay: number }) => {
  await new Promise<void>(async (resolve) => {
    for (let i = 0; BackgroundService.isRunning(); i++) {
      console.log({ index, iteration });
      if (index !== iteration) return;
      //your logic here
    }
  });
};

Ofcourse it's not an ideal solution but it works.

@Lunatic-coder001
Copy link

But know I think the problem of multiple task running simuntenously is solved and my code was this
useEffect(() => {
Linking.getInitialURL().then(url => {
console.log("uuu", url)
if (url !== null) {
// if opened from notification if app is killed

  }
}).catch(err => console.error('An error occurred', err));
let subcribtion = Linking.addEventListener('url', handleOpenURL);
subcribtion.subscriber;

return () => {
  subcribtion.remove();
};

}, []);
// Linking.addEventListener('url', handleOpenURL);

function handleOpenURL(evt) {
// Will be called when the notification is pressed foreground
console.log(evt.url);
// do something
}
is my code not working properly

@mlwong54
Copy link

Hi. Encounter the problem. I tried adding a delay function after the stop call.
Sometimes it works but sometimes the task is not properly killed.
There are still multiple task happening.

@Rapsssito Rapsssito added the duplicate This issue or pull request already exists label Jul 9, 2024
@Rapsssito Rapsssito added the bug Something isn't working label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

6 participants