简体   繁体   English

Firebase Firestore 在 for 循环中取消订阅快照

[英]Firebase Firestore Unsubscribe to SnapShot in for loop

I am working with firebase firestore and I have set of data from collection that I am taking in snapshot which is in for loop.我正在使用 firebase firestore,并且我有一组来自集合的数据,这些数据是我在 for 循环中拍摄的快照。

To avoid multiple time query the data base I have created data service mechnism to use the local array to display and I am handling it through rxjs subject behaviour.为了避免多次查询数据库,我创建了数据服务机制来使用本地数组进行显示,并且我正在通过 rxjs 主题行为来处理它。

Issue here is if user deleted some document from application I have removed it from firebase and then from local still its snapshot we have to manually unsubscribe that这里的问题是,如果用户从应用程序中删除了一些文档,我已经从 firebase 中删除了它,然后从本地仍然是它的快照,我们必须手动取消订阅

I know the basic method which is like this..我知道这样的基本方法..

var unsubscribe = db.collection("cities")
    .onSnapshot(function () {});
// ...
// Stop listening to changes
unsubscribe();

This is not applicable in my case as I am taking snapshot in for loop like this这不适用于我的情况,因为我正在像这样在 for 循环中拍摄快照

   getSnapShot() {
        this.global.currentUser.shortlist.forEach((propertyID: any) => {
          this.getSnapShotByID(ID);
        });
      }
    getSnapShotByID(ID) {
        // return new Observable((observe: Observer<any>) => {
        this.db.collection('properties').doc(propertyID).onSnapshot((snapshot: any) => {
//HERE I AM ADDING PROPERTY IN LOCAL ARRAY
    ....
    }

Now when user Delete Property from collection I have to unsubscribe otherwise it will keep listening and changing my local array现在,当用户从集合中删除属性时,我必须取消订阅,否则它将继续侦听和更改我的本地数组

any suggestion idea?有什么建议吗?

For your use case, I can recommend below approach对于您的用例,我可以推荐以下方法

As you are taking the entire copy of data in RXJS subject.因为您正在获取 RXJS 主题中的整个数据副本。 Where delete is happening inside your for loop delete the data which is not required from the existing Rxjs behaviour subject and again re push the new data after deletion by using the next operator.在 for 循环中发生删除的地方,删除现有 Rxjs 行为主题中不需要的数据,并在删除后使用 next 运算符再次重新推送新数据。 So your observable is also updated with the latest data along with firebase database因此,您的 observable 也会使用最新数据以及 firebase 数据库进行更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM