简体   繁体   中英

RxJava How to subscribe a Subject to an Observable

The documentation says, that a Subject is a Observer and can subscribe to Observables, but I can't find a way to do it in code.

Thanks in advance!

Sample junit code:

@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);
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM