Doua Beri
Doua Beri

Reputation: 10949

RxJava2 - observable.subscribe onComplete is Action type

In RxJava2 you can't do something like this

    observable.subscribe(s -> {
        System.out.println(s);
    }, e -> {
        System.err.println(e);
    }, c -> {
        System.err.println(c);
    });

because c-> {} is not Action type(obviously)

Instead you're forced to do something like this

    Action onComplete = new Action() {
        @Override
        public void run() throws Exception {
            System.out.println("on complete");
        }       
    };
    observable.subscribe(s -> {
        System.out.println(s);
    }, e -> {
        System.err.println(e);
    },onComplete);

Any reason why onComplete is not made as Consumer type?

Upvotes: 1

Views: 952

Answers (1)

Emanuel
Emanuel

Reputation: 8106

The reason is that onComplete doesnt emit any data. A Consumer requires a Type. If something is completed, it's completed.

If you have a Completable it only calls onComplete() which doesnt have emitted data aswell.

onCompleted really means completed. onNext/onSuccess means it has data emitted with Consumer<Type> while onError means it has a Consumer<Throwable>.

Upvotes: 3

Related Questions