RxJava如何订阅Observable的Subject

时间:2016-04-21 17:55:42

标签: java android rx-java rx-android

文档说,Subject是Observer并且可以订阅Observables,但是我找不到在代码中执行此操作的方法。

提前致谢!

1 个答案:

答案 0 :(得分:7)

样本junit代码:

@Test
public void shouldSubscribeToSubjectToObservable() throws InterruptedException {
    Observable<Integer> observable = Observable.just(1, 2);
    PublishSubject<Object> subject = PublishSubject.create();
    subject.subscribe(o -> {
        System.out.println("o = " + o);
    });

    observable.subscribe(subject);

    Thread.sleep(1000);
}