'Timeout in a react-native headless js task
I am developing a react-native applicaton with a headless js task to make some background work.
I need to execute some code after an amount of time, but noticed that using setTimeout execution gets suspended until the app is again in foreground.
Anyone has idea why this happens and know how to solve or workaround this?
EDIT
A simple example
module.exports = async (taskData) => {
for (let i = 0; i < 30; i++) {
setTimeout(function(){
console.log(i);
}, 1000*i + 100);
}
return;
}
When I start the app logs begins correctly to be printed in console, but when I put the app in background logs stops. As soon as the app is again in foreground all the remaining logs are printed together in console.
Solution 1:[1]
I faced the same issue and went into debugging it. The problem is with how the ReactContext is constructed when your app is in the background. The context signals to have been created BEFORE the TimingModule is initialized and your task is instantly executed by the HeadlessJsTaskService (before the TimingModule is initialized).
However the TimingModule adds a listener to the HeadlessJsTaskContext in order to only run while there are active background tasks. This listener is attached in TimingModule.initialize() - but due to the execution order this listener is attached after your task was already started, meaning it is never invoked and thereby the TimingModule is not running.
This can be fixed by declaring HeadlessJsTaskContext.addTaskEventListener(...) as synchronized and then invoking the added listeners for every currently active task.
Before:
public void addTaskEventListener(HeadlessJsTaskEventListener listener) {
mHeadlessJsTaskEventListeners.add(listener);
}
After:
public synchronized void addTaskEventListener(HeadlessJsTaskEventListener listener) {
mHeadlessJsTaskEventListeners.add(listener);
for (Integer activeTaskId: mActiveTasks) {
listener.onHeadlessJsTaskStart(activeTaskId);
}
}
Have you created an GitHub issue for this? I'd like to provide a PR with the fix and link it.
In case there's interest I could share a patched version of react-native.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marces Engel |
