I used \"Ionic Loading Controller\" to show a spinner until the data is retrieved then it calls \"dismiss()\" to dismissed it. it works fine, but sometimes when the app alre
This way it also resolved concurrent API call loader dismiss issue fix. You can call those functions to form the interceptor too. There is no fix duration, because if any call needs much time loader will continue. But if anyone gives duration then if that API won't stop by this time loader will stop
import { Injectable } from '@angular/core';
import { LoadingController } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class LoadingService {
isLoading = false;
loaderCounter = 0;
loading: HTMLIonLoadingElement;
constructor(public loadingController: LoadingController) {}
async present() {
this.loaderCounter = this.loaderCounter + 1;
if (this.loaderCounter === 1) {
this.isLoading = true;
const { loadingDuration, loadingMessage = loadingDefaultOptions.loadingMessage, loadingCssClass } = options;
this.loading = await this.loadingController.create({
duration: loadingDuration,
message: loadingMessage,
cssClass: loadingCssClass
});
await this.loading.present();
}
}
async dismiss() {
this.loaderCounter = this.loaderCounter - 1;
if (this.loaderCounter === 0) {
this.isLoading = false;
await this.loading.dismiss();
}
}
}