Just a generic question regarding the two Lifecycle Hooks OnInit and OnDestroy. As this article mentions, I had always assumed that OnInit is always run before the OnDestroy.
I have a case where in an ngOnDestroy() I'm stopping a background music track from playing. This sound is loaded the component's ngOnInit(), and because the ngOnDestroy() is being run without the ngOnInit() being run the sound object is undefined.
Code
ngOnInit() {
...
this.loadSounds();
...
}
ngOnDestroy() {
if (AppSettings.SOUNDS_ENABLED) {
this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC).fade(0.2, 0, 1500);
}
}
private loadSounds() {
this.soundService.loadSound(Sound.MINI_GAME_BG_MUSIC, SoundPathURL.MINI_GAME_BG_MUSIC, true, 0);
}
At the moment the code is failing in the ngOnDestroy when trying to .fade() a sound that's undefined. Of course I can easily fix this by checking that the sound is not undefined before executing the .fade() function. My assumption was that if an ngOnDestroy() is run the ngOnInit() must have been run as well - I guess I was wrong.
Now because of this case I'm thinking that in every ngOnDestroy() in my application I should check whether the object being used is undefined before performing any operations. So for example before unsubscribing from a subscription I should check if the subscription is undefined, and so on.
Am I right to assume so?