REVISED: I am working on a React Native app where I need to keep track of the timestamp when the app goes into the background in order to determine if re-login is required. I am using async storage to store this timestamp on the device, utilizing the appstate functionality. However, for some reason, the getItem and setItem code is not executing. Any thoughts on why that might be?
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState: any) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
this.getTime();
this.checkTimeStamp();
console.log(this.state.time, 'time var');
console.log('app is active, and has been in the background');
} else if (
this.state.appState == 'active' &&
nextAppState == 'active'
) {
this.setTime();
this.checkTimeStamp();
console.log(this.state.time, 'time var');
console.log('app is active, and just opened.');
} else {
this.setTime()
//sets the timestamp
console.log('app in background or closed');
}
this.setState({ appState: nextAppState });
};
async setTime() {
let seconds = this.generateTime();
try {
const val = await AsyncStorage.setItem('time', seconds.toString());
this.setState({ time: Number(val) });
} catch (error) {
console.error('onRejected function called: ' + error);
}
}
async getTime() {
let please = await AsyncStorage.getItem('time');
this.setState({ time: Number(please) });
console.log(this.state.time, 'time var');
}