【问题标题】:calling await inside react native background timer setInterval when app is in background当应用程序处于后台时,在反应本机后台计时器 setInterval 中调用 await
【发布时间】:2020-06-20 03:56:26
【问题描述】:

我在 react native 后台计时器 setInterval 中有一个 api 调用,它会导致代码执行暂停。如果我只是在回调中做简单的事情,即使应用程序在后台,它也可以正常工作,但是如果我在回调函数中使用 await 关键字,它会暂停执行。当应用程序成为前台时,所有任务都会立即执行

  const runTimer = () => {
    BackgroundTimer.start();
    timerInterval = BackgroundTimer.setInterval(async () => {
      await callAnApi();
    }, 20000);
  };

  const stopTimer = () => {
    BackgroundTimer.clearInterval(timerInterval);
    BackgroundTimer.stop();
  };

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您可以使用react-native-background-timer

    即使应用在后台也能正常工作。

    你可以找到here更多关于实现的细节

    【讨论】:

    • 在后台定时器中使用 await 导致暂停
    • 我想在最后一个完成之前不运行新的 api 调用。所以我需要一些异步 setInterval
    猜你喜欢
    • 2017-11-06
    • 2019-01-30
    • 1970-01-01
    • 2015-11-26
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多