user163588
user163588

Reputation: 137

RxJava subscribe Subject to Flowable

This might seem like an obvious question but I cannot seem to find the answer. I would like to subscribe a Subject to a Flowable but this method doesn't seem to be implemented:

Flowable<Long> flowable = Flowable.just(1L, 2L, 3L);
Subject<Long> subject = PublishSubject.create();
subject.subscribe(System.out::println);
flowable.subscribe(subject); // Method cannot be resolved

However for Observable it is implemented:

Observable<Long> observable = Observable.just(1L, 2L, 3L);
Subject<Long> subject = PublishSubject.create();
subject.subscribe(System.out::println);
observable.subscribe(subject); // Works

What am I missing? Is there an obvious reason as to why it isn't implemented? Are Flowable and Subject incompatible for some reason? Or is there another method to reach my goal?

Upvotes: 3

Views: 3963

Answers (1)

Grodriguez
Grodriguez

Reputation: 21995

A Subject is an Observer, so you can can subscribe it to an Observable but not to a Flowable.

Instead of using a PublishSubject, you can use a PublishProcessor (Api docs).

Upvotes: 2

Related Questions