joinObjects()
每次更新的值/列表到达时调用,而不是在固定的时间间隔内调用,这会产生大量开销。注意,我添加了新的实例变量,并将您的观察对象重命名为
favoritesList$
和
questionsList$
(美元后缀是一种良好的做法,表明它是可观察的(不是认购、价值等)。
public questions;
public favorites;
constructor(...) {
this.questionsList$ = this.afd.list('/questions/');
this.favoritesList$ = this.afd.list('/favorites/', {
query: {
orderByChild: 'user_id',
equalTo: userService.id,
},
});
this.questionsList$.subscribe(updatedList => {
this.questions = updatedList;
this.joinObjects();
});
this.favoritesList$.subscribe(updatedList => {
this.favorites = updatedList;
this.joinObjects();
});
}
joinObjects() {
let TempListX = [];
TempListX = this.questions.slice(0);
for (let i = 0; i < this.questions.length; i++) {
for (let j = 0; j < this.favorites.length; j++) {
if (this.questions[i].question_id == this.favorites[j].$key) {
TempListX[i].qTitle = this.favorites[j].title;
}
}
}
if (JSON.stringify(TempListX) === JSON.stringify(this.TempFavoritesList)) {
} else {
this.TempFavoritesList = TempListX.slice();
}
}
我希望这能让你更接近你的目标!