1

The problem is that I can use an Observer but I can't use a SingleObserver. It gives me this error:

Inferred type 'E' for type parameter 'E' is not within its bound; should implement 'io.reactivex.rxjava3.core.Observer<? super com.xxx.Network.Response.ResponseLogin>'

and

.subscribeWith(new SingleObserver<ResponseLogin>(){
                ^
  required: E
  found: <anonymous SingleObserver<ResponseLogin>>
  reason: inferred type does not conform to upper bound(s)
    inferred: <anonymous SingleObserver<ResponseLogin>>
    upper bound(s): Observer<? super ResponseLogin>
  where E,T are type-variables:
    E extends Observer<? super ResponseLogin> declared in method <E>subscribeWith(E)
    T extends Object declared in class Observable

Here are some implementation details:

//build.gradle
    implementation "io.reactivex.rxjava3:rxandroid:3.0.0"
    implementation "io.reactivex.rxjava3:rxjava:3.0.2"
//ApiInterface.java
import io.reactivex.rxjava3.core.Observable;
...
public interface ServerAPIInterface {
    // For POST request
    @FormUrlEncoded
    @POST("/login")
    public Observable<ResponseLogin> login(
        // the POST params to server
        @Field("user_email") String user_email,
        @Field("user_password") String user_password
    );

and the fault is at:

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
...
        myApiInterface
                .login(myUserEmail, myPassword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new SingleObserver<ResponseLogin>() {
                    @Override
                    public void onSuccess(ResponseLogin data) {
                        // update the adapter
                    }


                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                    }

                    @Override
                    public void onError(Throwable e) {
                        // display an error message
                    }
                });

If I replace SingleObserver with Observer (and implement all the required onXXX() methods it compiles OK. But I want a single observer.

Shall I downgrade to rxjava2 (untested)?

I suspect my imports are confused.

it seems there are 2 SingleObserver, another one is at io.reactivex.SingleObserver.

I did try to use these imports and skipped the rxjava3 prefix for most of them. However AndroidSchedulers can only be found in io.reactivex.rxjava3.android.schedulers.AndroidSchedulers.

bliako
  • 977
  • 1
  • 5
  • 16
  • 1
    `SingleObserver` does not extend `Observer` - http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Observer.html - use `.first(defaultItem).subscribeWith(SingleObserver() { .. });` – Mark Jan 26 '21 at 20:18
  • Thanks @MarkKeen. ```.first(null).subscribeWith(...)``` worked as you said. Should I stick to the ```Observer``` subclasses even if I have to make dummy ```onNext()```? or ```SingleObserver``` is the way to go if I expect a single record back? – bliako Jan 26 '21 at 20:56
  • 1
    Base it on the use case .. Stream of emissions = `Observable` / `Flowable`, one item = `Single`, optional item = `Maybe`. If you control the entire RX flow best to do that as soon as possible that way it makes it more explicit immediately and what is expected before any transformation functions in the chain (so your api returns a `Single` instead). Best not use `null` as default item - it will throw a NullPointerException if ever used as the single emission. You can use `firstOrError()` if required which will throw an explicit `NoSuchElementException` – Mark Jan 26 '21 at 22:04
  • got it, thanks for the advice, – bliako Jan 27 '21 at 08:35

0 Answers0