ionic
で初めてキャッシュを設定しています。ここでコードである:promiseが完了する前に実行されているPromise.allの()関数 - Ionic/Angular
listProducts(): Promise <any> {
let cacheKey = 'products';
this.cache.removeItem(cacheKey);
let promises_array:Array<any> = [];
let results;
return new Promise((resolve, reject) => {
this.cache.getItem(cacheKey).catch(() => {
this.list = this.af.list('/products');
this.subscription5 = this.list.subscribe(items => {
for (let x = 0; x < items.length; x++) {
promises_array.push(new Promise((resolve, reject) => {
console.log(JSON.stringify(items[x].customMetadata) + ": this is the customdata (((()()()()()");
let storageRef = firebase.storage().ref().child('/settings/' + items[x].customMetadata.username + '/profilepicture.png');
storageRef.getDownloadURL().then(url => {
console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!");
items[x].customMetadata.profilepic = url;
}).catch((e) => {
console.log("in caught url !!!!!!!$$$$$$$!!");
items[x].customMetadata.profilepic = 'assets/blankprof.png';
});
//this.startAtKey = item.$key;
this.productListArray.push(items[x].customMetadata);
}));
};
})
results = Promise.all(promises_array);
results.then(() => {
//setTimeout(() => {
console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray");
this.productListArray.reverse();
return this.cache.saveItem(cacheKey, this.productListArray);
//}, 3000);
})
}).then(data => {
console.log("Saved data: ", data);
resolve();
})
})
}
基本的に線console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray");
は、空の配列を印刷 - []
。すべての約束が完了する前にコードが実行されるべきではないので、この変数にデータがあるはずです。 setTimeout
がコメントアウトされていることに気づくでしょう - 私がコメントしたとき、それはうまくいきます - しかし、これは明らかに許容可能な解決策ではありません。どんな助けも素晴らしいだろう。
私のアプリではこのテクニックを実際に使っています...悪いことをやってみてください – ewizard