问题描述
我发现有两个用于Firestore的实时侦听器.
I found there's two realtime listener for firestore.
- Angularfire:snapshotChanges()
- Firestore javscript库:onSnapshot()
这是我的问题
- 我可以知道有什么区别吗?我应该如何正确使用它们(我正在使用Ionic + Cordova + Angular框架进行开发)?
- 如何分离snapshotChanges()?请参阅Firestore文档,我可以按照以下说明分离onSnapshot().
var unsubscribe = db.collection("cities")
.onSnapshot(function (){
// Respond to data
// ...
});
// Later ...
// Stop listening to changes
unsubscribe();
感谢您的友善分享.
推荐答案
AngularFire库不包含名为 onSnapshot()
的方法.使用 onSnapshot()
方法在javascript cloud firestore库中,以侦听实时更新.
AngularFire library does not contain a method called onSnapshot()
. The onSnapshot()
method is used in the javascript cloud firestore library, to listen for realtime updates.
尽管 snapshotChanges()
是专门用于angularfire的,但它会返回 Observable
,因此它将继续侦听数据库中的任何更改并检索数据.
While the snapshotChanges()
is specifically for angularfire it returns an Observable
therefore it will keep listening for any changes in the database and retrieve the data.
要取消订阅,只需调用方法 unsubscribe()
:
To unsubscribe
, you just need to call the method unsubscribe()
:
//Subscribe
subscription = this.itemRef.snapshotChanges().subscribe();
//Unsubscribe
subscription.unsubscribe()
这篇关于Angularfire snapshotChanges()与Firestore javscript库onSnapshot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!