maphongba008
maphongba008

Reputation: 2353

RxJava ignore onNext

I'm calling a url with Retrofit, and I don't care about the result. Is there anyway to ignore the onNext method?

Currently I'm using:

getRetrofit().create(UserAPI.class)
        .doSomething(a, b)
        .subscribeOn(Schedulers.newThread())
        .subscribe(t -> {}, Handler::handlerError);

As you can see, t -> {} do nothing, and I want to reduce it like:

getRetrofit().create(UserAPI.class)
            .doSomething(a, b)
            .subscribeOn(Schedulers.newThread())
            .subscribe(null, Handler::handlerError);

but get the error

java.lang.IllegalArgumentException: onNext can not be null

Upvotes: 2

Views: 796

Answers (2)

Jose Ignacio Acin Pozo
Jose Ignacio Acin Pozo

Reputation: 882

I don't think there is a way to avoid the onNext() implementation when subscribing to an observable if you want to have error handling as well. Alternatively you can create an Observer and reuse it in your subscribe calls, but you still need to implement the onNext() behaviour.

Example:

Observer errorObserver = Observers.create(t -> {}, Handler::handleError);

getRetrofit().create(UserAPI.class)
            .doSomething(a, b)
            .subscribeOn(Schedulers.newThread())
            .subscribe(errorObserver);

Upvotes: 0

Geralt_Encore
Geralt_Encore

Reputation: 3771

Instead of Observable you can return Completable from your Retrofit interface. It serves exactly same purpose you need.

Upvotes: 4

Related Questions