4

I've managed to create it this way:

private final PublishSubject<Void> subject = PublishSubject.create();

But how to pass value to it in onNext(T t)? I can't pass null to it, cause it will throw an exception. onComplete isn't an option too.

Max Makeichik
  • 227
  • 4
  • 18

2 Answers2

9

Nulls are generally not allowed in 2.x thus a Void type won't let you emit any onNext(null) unlike 1.x. If you think you need Void as your element type, it indicates you don't care about what elements are just that you want to react to something over and over.

For this case, you can instead use any other type and signal any value. In the wiki there is an example you can adapt:

enum Irrelevant { INSTANCE; }

PublishSubject<Irrelevant > source = PublishSubject.create();

source.subscribe(e -> { /* Ignored. */ }, Throwable::printStackTrace);

source.onNext(Irrelevant.INSTANCE);
source.onNext(Irrelevant.INSTANCE);
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • Thanks for your answer. I just thought maybe there are some built-in class es in the framework, that I could use. I think it's a good idea to add this functionality to the framework. – Max Makeichik Aug 30 '17 at 13:09
  • We don't have built in types for anyone's business object, otherwise imagine you'd be constrained by our `RxUser` object not having a certain field or structure... – akarnokd Aug 30 '17 at 13:48
3

In Kotlin you can use Unit:

val source = PublishSubject.create<Unit>()

source.onNext(Unit)
Diego Alvis
  • 1,785
  • 1
  • 13
  • 14