Java 如何取消订阅 PublishSubject 和 BehaviorSubject?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21257830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:44:00  来源:igfitidea点击:

How can PublishSubject and BehaviorSubject be unsubscribed from?

javarx-javasubject-observer

提问by arinte

Under the subjectspackage you have classes like PublishSubjectand BehaviorSubjectwhich I suppose can be described as some usable sample Observables.

下的subjects包你有这样的类PublishSubjectBehaviorSubject我想可以被描述为一些可用的样品Observables

How can these subjects be unsubscribed from? There is no unsubscribemethod and calling onCompletedends the Observable altogether right?

如何取消订阅这些主题?没有unsubscribe方法并且调用onCompleted完全结束 Observable 对吗?

采纳答案by Septem

A Subjectis an Observableand an Observerat the same time, it can be unsubscribed from just like normal observables. What makes subject special is that it is sort of bridge between observables and observers. It can pass through the items it observes by reemitting them, and it can also emit new items. Subjects are to observables as promises are to futures.

ASubject是 anObservable和 anObserver同时,它可以像普通的 observable 一样取消订阅。主题的特别之处在于它是可观察者和观察者之间的桥梁。它可以通过重新发射它观察到的项目来传递它们,也可以发射新的项目。主题之于可观察对象,就像承诺之于期货一样。

Here is a brief description of the subjects family:

以下是对科目族的简要说明:

AsyncSubject: only emits the last value of the source Observable

AsyncSubject:只发出源 Observable 的最后一个值

BehaviorSubject: emits the most recently emitted item and all the subsequent items of the source Observable when a observer subscribe to it.

BehaviorSubject:当观察者订阅源 Observable 时,发射最近发射的项目和所有后续项目。

PublishSubject: emits all the subsequent items of the source Observable at the time of the subscription.

PublishSubject:在订阅时发出源 Observable 的所有后续项目。

ReplaySubject: emits all the items of the source Observable, regardless of when the subscriber subscribes.

ReplaySubject:发出源 Observable 的所有项目,无论订阅者何时订阅。

the official doccomes with some nice marble diagrams which makes it more easier to understand

官方文档提供了一些好的大理石图这使得它更容易理解

回答by martiansnoop

Subjects are essentially are bothObservables and Observers.

主体本质上既是可观察者又是观察者。

An Observable is essentially a thing that has a function that takes an Observer and returns a subscription. So, for example, given simple observable:

Observable 本质上是一个函数,它接受一个观察者并返回一个订阅。因此,例如,给定简单的 observable:

    Observable<Integer> observable = Observable.create(new Observable.OnSubscribeFunc<Integer>() {
        @Override
        public Subscription onSubscribe(Observer<? super Integer> observer) {
            observer.onNext(3);
            observer.onNext(2);
            observer.onNext(1);
            observer.onCompleted();

            return Subscriptions.empty();
        }
    });

And here we would subscribe to it, to print out a line for each integer:

在这里,我们将订阅它,为每个整数打印一行:

    Subscription sub = observable.subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            System.out.println(integer);
        }
    });

You would unsubscribe on the above by calling sub.unsubscribe().

您可以通过调用取消订阅上述内容sub.unsubscribe()

Here is a PublishSubject that does roughly the same thing:

这是一个 PublishSubject 做大致相同的事情:

    PublishSubject<Integer> publishSubject = PublishSubject.create();
    Subscription subscription = publishSubject.subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            System.out.println(integer);
        }
    });

    publishSubject.onNext(3);
    publishSubject.onNext(2);
    publishSubject.onNext(1);

You would unsubscribe from it the same way, by calling subscription.unsubscribe().

您可以通过调用 以同样的方式取消订阅subscription.unsubscribe()

回答by Praveer Gupta

All Subjectsextend Observablewhich you can subscribe to using any of the multiple subscribe(...)methods. Call to any of the subscribe(...)methods returns a Subscription.

您可以使用多种方法中的任何一种来订阅所有Subjects扩展。调用任何方法都会返回一个.Observablesubscribe(...)subscribe(...)Subscription

Subscription subscription = anySubject.subscribe(...);

Use this subscriptioninstance's unsubscribe()method when you want to stop listening to the events from the Subject.

当您想停止侦听来自主题的事件时,请使用此subscription实例的unsubscribe()方法。

subscription.unsubscribe();