【发布时间】:2018-04-05 21:19:05
【问题描述】:
我第一次使用ionic 设置缓存。代码如下:
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 被注释掉了——当我评论它时,它起作用了——但这显然不是一个可接受的解决方案,我只是表明它是一个与承诺有关的时间问题。任何帮助都会很棒。
【问题讨论】:
标签: angular typescript caching ionic-framework promise