angularfire2 中的 once listener 等价物是什么?
What is the once listener equivalent in angularfire2?
我在想,我们可以这样说吗:
this.af.database.object('whatever').first().toPromise()
相当于:
firebase.database().ref('whatever').once('value', snapshot=>{})
我知道 firebase 会缓存数据(在内存中),只要该数据有一个活动的侦听器。
所以在once listener的情况下,数据从缓存中被清除。
当我使用 first().toPromise() 时,这是相同的行为吗,那么它是否会在接收到数据时立即分离侦听器?
我问你这个问题是因为我经常使用异步,我不想在性能上有所损失。
从 this Github issue 的回答来看,take(1)
似乎会用 once()
创建一个可观察对象。从那里:
var team = this.af.database.object('/teams/' , { preserveSnapshot: true }).take(1);
async fetchUserRecord(userId: string) {
const snapshot = await this.firedb.database.ref('users').child(userId).once('value');
console.log(snapshot.val());
}
这是一个迟到的答案,但是,在 angularfire 2
,您可以使用上述函数查询一次数据,而不是监听事件或快照更改。
我在想,我们可以这样说吗:
this.af.database.object('whatever').first().toPromise()
相当于:
firebase.database().ref('whatever').once('value', snapshot=>{})
我知道 firebase 会缓存数据(在内存中),只要该数据有一个活动的侦听器。 所以在once listener的情况下,数据从缓存中被清除。 当我使用 first().toPromise() 时,这是相同的行为吗,那么它是否会在接收到数据时立即分离侦听器?
我问你这个问题是因为我经常使用异步,我不想在性能上有所损失。
从 this Github issue 的回答来看,take(1)
似乎会用 once()
创建一个可观察对象。从那里:
var team = this.af.database.object('/teams/' , { preserveSnapshot: true }).take(1);
async fetchUserRecord(userId: string) {
const snapshot = await this.firedb.database.ref('users').child(userId).once('value');
console.log(snapshot.val());
}
这是一个迟到的答案,但是,在 angularfire 2
,您可以使用上述函数查询一次数据,而不是监听事件或快照更改。