可观察的创建被调用两次 [英] Observable create is called twice

查看:43
本文介绍了可观察的创建被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Ionic3与rxjs/Observable一起使用.我有以下函数,由于某种原因,即使该函数仅被调用一次,第三行也会被触发两次.

I am using Ionic3 with a rxjs/Observable. I have the following function, and for some reason, even though the function is only called once, the 3rd line gets fired twice.

findChats(): Observable<any[]> {
    return Observable.create((observer) => {
        this.chatSubscription2 = this.firebaseDataService.findChats().subscribe(firebaseItems => {
            this.localDataService.findChats().then((localItems: any[]) => {
                let mergedItems: any[] = [];
                if (localItems && localItems != null && firebaseItems && firebaseItems != null) {
                    for (let i: number = 0; i < localItems.length; i++) {
                        if (localItems[i] === null) {
                            localItems.splice(i, 1);
                        }
                    }
                    mergedItems = this.arrayUnique(firebaseItems.concat(localItems), true);
                } else if (firebaseItems && firebaseItems != null) {
                    mergedItems = firebaseItems;
                } else if (localItems && localItems != null) {
                    mergedItems = localItems;
                }
                mergedItems.sort((a, b) => {
                    return parseFloat(a.negativtimestamp) - parseFloat(b.negativtimestamp);
                });
                observer.next(mergedItems);
                this.checkChats(firebaseItems, localItems);
            });
        });
    });
}

问题

这引起了一个问题,因为this.chatSubscription2正在获取第二个订阅的值,并且第一个订阅丢失了,不允许我取消订阅.

This is causing a problem because this.chatSubscription2 is taking the value of the second subscription, and the first subscription gets lost, not allowing me to ever unsubscribe.

line 2 is executed once
line 3 is executed twice

问题

如何创建只有一个订阅的Observable?

How do I create an Observable with only one subscription?

谢谢

更新

我使用share()将代码更改为以下代码,但第三行仍然执行两次:

I change the code to the following using share(), but the 3rd line still gets executed twice:

findChats(): Observable<any[]> {
    return Observable.create((observer) => {
        const obs = this.firebaseDataService.findChats().share();
        this.chatSubscription2 = obs.subscribe(firebaseItems => {
                     ....

推荐答案

正如其他用户所建议的那样,尽管findChats仅被调用一次,但它返回的可观察对象似乎已被预订多次. create返回一个可观察到的冷现象,这将导致对每个订阅执行所有内部逻辑.您可以在整个过程的最后(在create调用之后/外面)敲打share进行测试,但是我建议如果您根本不使用create,则解决方案实际上会更简单,而不是只映射/flatMapped/switch将原始流映射到所需的流中(以避免手动订阅管理).

As other users have suggested, while findChats is only called once, it would seem that the observable it returns is subscribed to multiple times. create returns a cold observable which will cause all the internal logic to be executed for each subscription. You can whack a share on the end of the whole thing (i.e. outside / after the create call) to test this, but I would suggest the solution would actually be simpler if you did not use create at all, and rather just mapped / flatMapped / switchMapped your original stream into your desired stream (to avoid manual subscription management).

这篇关于可观察的创建被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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