Angularfire snapshotChanges() vs Firestore javscript library onSnapshot()

蹲街弑〆低调 提交于 2021-01-29 21:20:50

问题


I found there's two realtime listener for firestore.

  • Angularfire: snapshotChanges()
  • Firestore javscript library: onSnapshot()

Here is my question

  1. May I know what is the difference? How should I use them properly (I'm developing using Ionic + Cordova + Angular framework)?
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!