问题
I found there's two realtime listener for firestore.
- Angularfire: snapshotChanges()
- Firestore javscript library: onSnapshot()
Here is my question
- May I know what is the difference? How should I use them properly (I'm developing using Ionic + Cordova + Angular framework)?
- How to detach snapshotChanges()? Refer to Firestore documentation, I can detach onSnapshot() as per below.
var unsubscribe = db.collection("cities")
.onSnapshot(function (){
// Respond to data
// ...
});
// Later ...
// Stop listening to changes
unsubscribe();
Thanks for your kind sharing.
回答1:
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.
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.
To unsubscribe
, you just need to call the method unsubscribe():
//Subscribe
subscription = this.itemRef.snapshotChanges().subscribe();
//Unsubscribe
subscription.unsubscribe()
来源:https://stackoverflow.com/questions/61054405/angularfire-snapshotchanges-vs-firestore-javscript-library-onsnapshot