Javascript 处理React Native上的internet连接丢失
我制作了一个React本机应用程序,在Android上使用前台服务扫描BLE设备。我面临的问题是互联网连通性的丧失。一旦internet可用,当internet不可用时添加到队列中的挂起操作将不会执行 一旦检测到信标,我将使用NativeModules发送一个事件以本机方式作出反应,在收到事件后,我将检查internet是否可用。如果不是,则存储在队列中Javascript 处理React Native上的internet连接丢失,javascript,android,react-native,redux,altbeacon,Javascript,Android,React Native,Redux,Altbeacon,我制作了一个React本机应用程序,在Android上使用前台服务扫描BLE设备。我面临的问题是互联网连通性的丧失。一旦internet可用,当internet不可用时添加到队列中的挂起操作将不会执行 一旦检测到信标,我将使用NativeModules发送一个事件以本机方式作出反应,在收到事件后,我将检查internet是否可用。如果不是,则存储在队列中 const didEnterRegion = async (store, beacons) => { const {
const didEnterRegion = async (store, beacons) => {
const { isConnected } = store.getState().connection;
if (isConnected) {
store.dispatch({
type: REQUEST_CHECKIN,
payload: beacons,
});
} else {
NativeModules.BeaconFinder.sendNotification(
'Looks like you entered a venue, but the internet is not reachable.',
beacons.uuid.toString(),
);
store.dispatch({
type: ADD_ACTION_TO_QUEUE,
name: REQUEST_CHECKIN,
payload: beacons,
});
}
};
在App.js中,我经常检查互联网连接。若可用,我将执行队列中的所有操作
export default class App extends Component {
componentDidMount() {
const unsubscribe = NetInfo.addEventListener(state => {
this._handleConnectionChange(state.isInternetReachable);
});
}
componentWillUnmount() {
//unsubscribe();
}
_handleConnectionChange = isConnected => {
if (isConnected && _.size(store.getState().connection.actionQueue) > 0) {
let queue = store.getState().connection.actionQueue;
_.map(queue, action => {
console.log(action);
store.dispatch({
type: action.name,
payload: action.payload
});
});
}
store.dispatch({
type: CHANGE_CONNECTION_STATUS,
isConnected
});
};
render...
}
当应用程序位于前台时,此功能有效,但当应用程序从任务切换器中被终止时,代码会随机执行,即有时工作,有时不工作。Redux不是持久存储。一旦应用程序关闭,下次打开应用程序时,它将加载默认的redux状态。但我使用的是redux persist@MohammedashFaqWhat存储引擎。AsyncStorage@MohammedAshfaqRedux不是持久存储。一旦应用程序关闭,下次打开应用程序时,它将加载默认的redux状态。但我使用的是redux persist@MohammedAshfaq您正在使用哪个存储引擎。AsyncStorage@MohammedAshfaq