I have to go through an array of items and fetch the prices in the realtime database, but I want to show a loading message on start and after I finish loading I want to remove the loading message, but the way I did the message does not work its removed before the end of loading:
let loading = this.loadingCtrl.create({
content: 'Waiting...'
});
loading.present();
for (let i = 0; i <= this.Cart.length - 1; i++) {
let product = this.db.object('/menuItems/' + this.Cart[i].item.itemId);
product.valueChanges().subscribe((prodData: any) => {
if (prodData != null) {
this.Cart[i].item.price = prodData.price[0];
}
});
}
loading.dismiss();
How can I do this correctly?
Promise.allto fetch those prices in parallel best practise, i.e., push each db read into an array thenPromise.allthat array. When the promise resolves, hide the indeterminate loader.var myArray = []then for each read domyArray.push(firebase.firestore().collection("prices").doc(itemId).get()). Then after that iteration, after you fill the array, doPromise.all(myArray).then. The return values will be sitting at the index. (remember if a single read fails, the whole Promise.all fails)