首页 > 解决方案 > 如何在失败/没有互联网时暂停网络请求并在互联网可用时执行?

问题描述

我正在开发一个 React Native + redux 应用程序。该应用程序使用 android 中的前台服务来扫描 BLE 信标。当服务通过网桥发现信标以响应本机时,该服务会发送事件,在接收到事件后,完成获取相关信息的 API 调用。

问题是当没有互联网并且发现信标时。我正在使用 react-native-storage 来存储找到的信标。

storage.save({
key: 'places', // Note: Do not use underscore("_") in key!
id: beacons.uuid,
data: {
  uuid: beacons.uuid.toString(),
  minor: parseInt(beacons.minor, 10),
  major: parseInt(beacons.major, 10),
},

// if expires not specified, the defaultExpires will be applied instead.
// if set to null, then it will never expire.
expires: null,
});

如果信标超出范围,我会从存储中移除信标。当应用程序重新启动时,我检查存储是否有一些信标并为这些信标进行 API 调用。

如果没有互联网,则请求失败。有没有办法暂停请求并在有互联网时重新执行它?

标签: javascriptandroidiosreact-nativealtbeacon

解决方案


推荐阅读