hello,
I am using Ionic v3.3.0, and am having a problem with the application initialization/ startup.
I have placed initialization code in the constructor of “MyApp” (the root component) in app.component.ts. and set the “this.platform.ready()” as the last step in the initialization sequence.
see the code snippet below
constructor(private platform: Platform, private menu: MenuController, private splash: SplashScreen, private statusb: StatusBar,
private device: Device, private storage: Storage, private oauth2: Oauth2, private events: Events, private user: User) {
// Check if the user has already seen the tutorial
this.storage.ready()
.then(() => this.setRoot())
.then(() => this.getKeys())
.then(() => this.setLocale())
.then(() => this.setId())
.then(() => this.setAccessToken())
.then(() => this.setAnonymous())
.then(() => this.splash.hide())
.then(() => this.statusb.styleDefault())
.then(() => this.setCurrentPAge())
.then(() => this.platform.ready());
these operations are mainly concerned with the initialization/restoration of the application wide parameters/configurations (which had been stored in the storage). for example, the access token, the login status ,etc…
I have designed/implemented thus that there is an automatic fetching of data from the server upon entering the default page,
seen below
constructor(private nav: NavController, private events: Events, private gabriel: Gabriel,
private valet: MsgsValet, private user: User, private popup: Popup){
//set the handlers
this._insert = (msgs) => {this.insert(msgs);};
this._fetch = (ids) => {this.fetch(ids);};
this._remove = (ids) => {this.remove(ids);};
this._override = (od) => {this.override(od);};
//subscribe
this.events.subscribe(Cons.EVT_HI_MSGS, this._insert);
this.events.subscribe(Cons.EVT_MSGS_CHANGED, this._fetch);
this.events.subscribe(Cons.EVT_MSGS_DELETED, this._remove);
this.events.subscribe(Cons.EVT_MSGS_OVERRIDE, this._override);
this.fetch([]);
this.user.currentPage = Pages.Messages;
}
the syndrome of problem is that the first attempt of fetching data upon startup always fails.
I traced into the code, and found out that the data fetching operation had occurred even before the initialization finishing.
parameters such as login status, access token, and user id, etc are still “undefined”, which have caused a “401” error.
I tried to maintain a strict execution sequence by adopting the async-await mechanism in the initialization/restore functions(see below), yet had not had any effect.
private async setRoot(){
let noTutorial = await this.storage.get(Cons.noTutorial);
if(noTutorial) {
this.rootPage = TabsPage;
}else{
this.rootPage = TutorialPage;
}
}
private async getKeys(){
this.keys = await this.storage.keys()
}
private async setLocale(){
let locale;
if(this.hasKey(Cons.locale)){
locale = await this.storage.get(Cons.locale);
}else{
locale = 'zh';
this.storage.set(Cons.locale, locale)
}
this.user._locale = locale;
}
so, my question: how may I be able to control the initialization and startup sequence to make sure that initialization always finishes before service starts.
thanks