【问题标题】:How to run background task even device restarted in react native?即使设备在本机反应中重新启动,如何运行后台任务?
【发布时间】:2018-12-16 22:02:40
【问题描述】:

我想制作一个在后台从服务器获取数据的应用。我使用 react-native-queue 但设备重新启动时它不起作用。有什么解决办法吗?

export default class Stores extends Component<{}> {

constructor(props){
    super(props);
    this.state = { nodes: [], isFetching: false, mailListener:{connecting:false},queue: null};
  }

componentDidMount(){
    this.init();
  }

async init() {
    const queue = await queueFactory();
    const present = this;

    queue.addWorker('listener', async (id, payload) => {

      //my jobs here

      await new Promise((resolve) => {
        setTimeout(async () => {
          // Keep creating these jobs until counter reaches 3.
            queue.createJob('listener');
          resolve();
        }, 3000);
      });
    });

    queue.createJob('listener');

    queue.start();

    // Attach initialized queue to state.
    this.setState({
      queue
    });
  }

}

【问题讨论】:

    标签: react-native queue background-task


    【解决方案1】:

    您可以使用 react-native-threads 之类的东西,但没有什么可以阻止用户关闭您的应用程序。每当您重新启动设备时,也会发生这种情况。应用程序关闭时无需获取内容,那么您可以在应用程序打开时进行获取,以便用户始终看到最新信息吗? 我在我的应用中有类似的任务,以下是我的一些其他建议:

    -让用户自己刷新数据(我用拉刷新列表)

    -使后端发出事件并且您的应用可以监听它们,这样您就不需要同步

    -后台同步任务的间隔不能小于 20 分钟。如果您因为电池耗尽而这样做,您的应用将被应用商店拒绝。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多