代码之家  ›  专栏  ›  技术社区  ›  Runtime Terror

如何正确取消订阅此类obervable

  •  2
  • Runtime Terror  · 技术社区  · 6 年前

    我有一个 getDimensions 返回 Observable<string[]> .

    尺寸.服务.ts

    public getDimensions(id: string): Observable<string[]> {
        return this.service.loadDimensions(id);
    }
    

    重新加载服务.ts

    public update(idsToUpdate: string[]): void {
    
        idsToUpdate.map(id => this.dimensionService.getDimensions(id)).pipe(
            map(
                 newDimensions => this.updateDimensions(newDimensions)
            )
        ).subscribe(); // ToDo: unsubscribe
    
    }
    

    然后在应用程序的另一部分,我在内部调用这个方法 update 方法。问题是,我不知道如何正确取消订阅 获取维度 . 一个可能的解决方案是,创建一个 Subscription 然后在 OnDestroy 呼叫取消订阅,但对我来说这不是一个好的解决方案:

    • 这是一个服务,我在整个应用程序中使用它,因此 OnDestroy公司 钩子永远不会发生
    • 我打电话给 更新 方法间隔 n 秒,所以每一秒 n 秒新订阅

    可能的解决方案: (不好)

    重新加载服务.ts

    private subscriptions: Subscription = new Subscription();
    
    ...
    ...
    
    public update(idsToUpdate: string[]): void {
    
        const sub = idsToUpdate.map(id => this.dimensionService.getDimensions(id)).pipe(
            map(
                 newDimensions => this.updateDimensions(newDimensions)
            )
        ).subscribe(); // ToDo: unsubscribe
    
        this.subscription.add(sub);
    
    }
    
    ...
    ...
    
    public onDestroy(): void {
        this.subscription.unsubscribe();
    }
    

    编辑:

    正如@jgerstle在他的评论中提到的那样,如果观察表完成(这是我的情况),就不需要取消订阅。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Muhammed Albarmavi    6 年前

    如果您想在获得数据或可观察数据完成后取消订阅

    public update(idsToUpdate: string[]): void {
    
        const sub = idsToUpdate.map(id => this.dimensionService.getDimensions(id)).pipe(
            map(
                 newDimensions => this.updateDimensions(newDimensions)
            )
        ).subscribe(() => sub.unsubscribe() ); // or you can use complate 
    
    }
    
        2
  •  0
  •   John    6 年前

    订阅的添加功能可能不会执行您尝试执行的操作。 从文件中可以看出以下情况。

       /**
         * Adds a tear down to be called during the unsubscribe() of this
         * Subscription.
         *
         * If the tear down being added is a subscription that is already
         * unsubscribed, is the same reference `add` is being called on, or is
         * `Subscription.EMPTY`, it will not be added.
         *
         * If this subscription is already in an `closed` state, the passed
         * tear down logic will be executed immediately.
         *
         * @param {TeardownLogic} teardown The additional logic to execute on
         * teardown.
         * @return {Subscription} Returns the Subscription used or created to be
         * added to the inner subscriptions list. This Subscription can be used with
         * `remove()` to remove the passed teardown logic from the inner subscriptions
         * list.
         */
        add(teardown: TeardownLogic): Subscription;
    

    我猜你想做的是退订 idsToUpdate -订阅。您可以像这样保留订阅的引用,然后执行 sub.unsubscribe()

    下面是一个修改代码的示例

    private sub: Subscription;
    
    ...
    ...
    
    public update(idsToUpdate: string[]): void {
    
        this.sub = idsToUpdate.map(id => this.dimensionService.getDimensions(id)).pipe(
            map(
                 newDimensions => this.updateDimensions(newDimensions)
            )
        ).subscribe(); // ToDo: unsubscribe
    
    }
    
    ...
    ...
    
    public onDestroy(): void {
        if(this.sub) this.sub.unsubscribe();
    
    }