Angularfire snapshotChanges() 与 Firestore javscript 库 onSnapshot() [英] Angularfire snapshotChanges() vs Firestore javscript library onSnapshot()

查看:24
本文介绍了Angularfire snapshotChanges() 与 Firestore javscript 库 onSnapshot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 firestore 有两个实时侦听器.

I found there's two realtime listener for firestore.

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

这是我的问题

  1. 我可以知道有什么区别吗?我应该如何正确使用它们(我正在使用 Ionic + Cordova + Angular 框架进行开发)?
  2. 如何分离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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆