ionic
으로 처음으로 캐싱을 설정합니다. 여기에 코드입니다 :약속이 끝나기 전에 실행되는 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