javascript - 如何在失败/没有互联网时暂停网络请求并在互联网可用时执行?
问题描述
我正在开发一个 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 调用。
如果没有互联网,则请求失败。有没有办法暂停请求并在有互联网时重新执行它?
解决方案
推荐阅读
- c++ - C++ 我怀疑这个函数的安全性
- javascript - 可以使用 JavaScript 设置 PsuedoClass 和通配符 CSS 吗?
- javascript - 此 javascript 片段中方括号的术语/概念是什么?
- vue.js - 如何根据计算属性显示排序图标?
- angular - 带有 Angular 的 html2canvas 无法按预期工作
- c# - 在画布元素上调用 onclick 时出现问题 (Unity3d)
- django - 在模型对象的 Django 管理界面中显示来自相关模型的数据
- algorithm - 如何使排序与排名稳定?
- hibernate - 春季启动数据 jpa DataIntegrityViolationException EntityExistsException
- javascript - 如何更新 MapBox 中的单个 GeoJson Feature 属性并实时反映?