简体   繁体   English

为什么删除 BehaviourSubject 会导致我的服务延迟 3-4 秒?

[英]Why does removing BehaviourSubject cause my service to delay for 3-4 seconds?

I had earlier created a BehaviourSubject and on subscribing it I was making a service call.我之前创建了一个BehaviourSubject并且在订阅它时我正在拨打服务电话。 But, due to the nature of BehaviorSubject , there was an unnecessary on the initialisation of the BehaviouSubject .但是,由于 BehaviorSubject 的性质, BehaviorSubject的初始化是BehaviouSubject的。 So, on further reading, I changed BehaviourSubjedct to ReplaySubject(1) .因此,在进一步阅读时,我将BehaviourSubjedct更改为ReplaySubject(1)

Now, In the main @Injectable class where I have defined the services, there is another service which is independent, that gets delayed once I made the above changes.现在,在我定义了服务的主要@Injectable class 中,还有另一个独立的服务,一旦我进行了上述更改,就会延迟。 How do I determine the cause of the issue here?我如何在这里确定问题的原因?

component.ts组件.ts

constructor(private _subjectService: SubjectService, private _s1: Service1){
    this._s1.Call1();
    this.testFunction();
}

testFunction(){
this._subjectService.GetData().subscribe(res => {
      if (res !== null) {
         this._s1.Call2();  
     }
   }
}   

SubjectService主题服务

  // filterSubject = new BehaviorSubject(<KendoRequestVM>{});
  // filterDataSubject = new BehaviorSubject(<KendoRequestVM>{});

   filterSubject = new ReplaySubject<KendoRequestVM>(1);
   filterDataSubject = new ReplaySubject<KendoRequestVM>(1);

Here, Call1() is getting called when I use BehaviouSubject instead of ReplaySubject .在这里,当我使用BehaviouSubject而不是ReplaySubject时,会调用 Call1() 。

A behavior subject will always return immediately (synchronously even).行为主体总是会立即返回(甚至同步)。 A replay subject will only return immediately / synchronously if it has a value.重播主题只有在有值的情况下才会立即/同步返回。 It's impossible to tell with just the code you've shared because you haven't shared the part that is populating the subjects (eg calling next ) but that seems like the most likely explanation.仅使用您共享的代码是不可能的,因为您没有共享填充主题的部分(例如调用next ),但这似乎是最有可能的解释。

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

相关问题 以n秒延迟重试轮询服务 - Retrying a polling service with n seconds delay 为什么在应用程序组件中进行服务注入会导致URL参数未定义? - Why does service injection in app component cause undefined for URL parameters? Angular2中的共享服务和可观察的BehaviourSubject - Shared service and Observable, BehaviourSubject in Angular2 如何使用 behaviorsubject 更改服务中的参数 function - How to use behavioursubject to change parameters in service function 为什么在BehaviourSubject上订阅,因为Observable从不触发onComplete? - Why subscribe on BehaviourSubject asObservable never fires onComplete? Angular8:当提取到另一个服务时,BehaviourSubject 部分不起作用 - Angular8: BehaviourSubject partially not working when extracting to another Service 由于服务范围,无法从BehaviourSubject获取新值 - Cannot get new value from BehaviourSubject because of service scope 使用 Subject 或 BehaviourSubject 通过服务提醒组件发生了什么事? - Use Subject or BehaviourSubject to alert a component, through a service, that something happened? 如何修复服务到组件之间的行为主题订阅问题 - How to fix Behavioursubject subscription issue from service to component 如何在服务中对 BehaviourSubject 变量进行单元测试,它是组件中的可观察对象 - How to do Unit Testring of a BehaviourSubject variable in a service, it is an observable in a component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM