简体   繁体   English

RxJava忽略onNext

[英]RxJava ignore onNext

I'm calling a url with Retrofit, and I don't care about the result. 我正在使用Retrofit调用url,但我不在乎结果。 Is there anyway to ignore the onNext method? 无论如何,有没有忽略onNext方法?

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: 如您所见, t -> {}不执行任何操作,而我想将其减少为:

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

Instead of Observable you can return Completable from your Retrofit interface. 可以从Retrofit接口返回Completable而不是Observable It serves exactly same purpose you need. 它完全满足您所需的目的。

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. 我认为,如果您也想进行错误处理,那么在订阅可观察对象时,没有办法避免onNext()实现。 Alternatively you can create an Observer and reuse it in your subscribe calls, but you still need to implement the onNext() behaviour. 另外,您可以创建一个Observer并在您的subscribe调用中重用它,但是您仍然需要实现onNext()行为。

Example: 例:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM