Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava subscribe Subject to Flowable

Tags:

rx-java2

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?

like image 701
user163588 Avatar asked Jan 29 '26 23:01

user163588


1 Answers

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).

like image 92
Grodriguez Avatar answered Feb 01 '26 00:02

Grodriguez