I have a number of variables that get set when someone logs into my App, these then get used throughout the application.
The majority of the time this works great but sometimes on iOS devices (this doesn’t seem to affect Android) when the App is resumed from the background these variables are lost therefore causing errors.
Does anyone know why this happens or what the best solution is to the problem?
I do also store these variables in local storage so possibly some sort of getter/setter that can check if the value is null or undefined and if so pull it from local storage. If this is the case then I would imagine I will need to do quite a bit of reworking because I would need to wait for the local storage promise to return the value (using “then”) and I would not be able to use the current method where I am just referencing the variable directly (code examples below).
Below is an example of class I use to store the variables:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class GlobalProvider {
// Global variables
public authToken: string;
public customerId: string;
public version: any;
public build: any;
}
The following shows how I set the “customerId” in local storage but also set it on the GlobalProvider class shown above:
// This gets called when the user logs in
this.authLocal.setCustomerId(1234);
// This is the method that gets called above and sets the customer ID in
// local storage but also sets in on the GlobalProvider class
setCustomerId(customerId) {
return new Promise((resolve) => {
this.storage.ready().then(() => {
this.storage.set('customerId', customerId).then((value) => {
// Set global variable
this.global.customerId = value;
// Return true
resolve(true);
});
});
});
}
Below is how I then get and use the “customerId” throughout my App but as stated above, sometimes on iOS devices when the App resumes from the background this value is undefined:
const test = 'customerid=' + this.global.customerId;
Has anyone had this problem before or got any thoughts as the best practice to resolve it, ideally without having to put everything in a “then” after it has retrieved the value from local storage?
1 post - 1 participant