简体   繁体   中英

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.

module.exports = async (taskData) => {

    for (let i = 0; i < 30; i++) {
        setTimeout(function(){
            console.log(i);            
        }, 1000*i + 100);
    }

    return;
}

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<\/code> is initialized and your task is instantly executed by the HeadlessJsTaskService<\/code> (before the TimingModule<\/code> is initialized).

This listener is attached in TimingModule.initialize()<\/code> - 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<\/code> is not running.

Before:

  public void addTaskEventListener(HeadlessJsTaskEventListener listener) {
    mHeadlessJsTaskEventListeners.add(listener);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM