BehaviorSubject vs PublishSubject

后端 未结 4 1343
北海茫月
北海茫月 2021-02-05 01:29

I\'m trying to get my head around the golden rule (if any) about:

When to use BehaviorSubject ?

and

When to

4条回答
  •  太阳男子
    2021-02-05 02:11

    The main difference between PublishSubject and BehaviorSubject is that the latter one remembers the last emitted item. Because of that BehaviorSubject is really useful when you want to emit states

    Why they make project field a BehaviorSubject and not PublishSubject ?

    Probably because they want to be able to retrieve the last emitted project with this method:

    @Override public @NonNull Observable project() {
      return this.project;
    }
    

提交回复
热议问题