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!
A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. Every Subject is an Observable. Given a Subject, you can subscribe to it, providing an Observer, which will start receiving values normally.
If I understood correctly, you want to convert Single<List<Item>> into stream of Item2 objects, and be able to work with them sequentially. In this case, you need to transform list into observable that sequentially emits items using . toObservable(). flatMap(...) to change the type of the observable.
onNext(): This method is called when a new item is emitted from the Observable. onError(): This method is called when an error occurs and the emission of data is not successfully completed. onComplete(): This method is called when the Observable has successfully completed emitting all items.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With