androidretrofit2rx-java2retrolambda

RxJava1 to Rxjava2 Migration Error


Converting RxJava1 code to RXJava2 Giving this error, not sure what i'm doing wrong

no instance(s) of type variable(s) R exist so that Single> conforms to ObserableSource

API Call

@GET("/search/users?per_page=2")
Observable<UsersList> searchGithubUsers(@Query("q") String searchTerm);

@GET("/users/{username}")
Observable<User> getUser(@Path("username") String username);

RxJAva1:

 public Observable<List<User>> searchUsers(final String searchTerm) {
        return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
                usersList -> Observable.from(usersList.getItems())
                        .concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
                .retryWhen(observable -> observable.flatMap(o -> {
                    if (o instanceof IOException) {
                        return Observable.just(null);
                    }
                    return Observable.error(o);
                }));
    }

RXJava2

   public Observable<List<User>> searchUsers(final String searchTerm) {
        return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
                usersList -> Observable.fromIterable(usersList.getItems())
                        .concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
                .retryWhen(observable -> observable.flatMap(o -> {
                    if (o instanceof IOException) {
                        return Observable.just(null);
                    }
                    return Observable.error(o);
                }));
    }

Solution

  • toList returns Single in 2.x; you have to apply toObservable() after it. Note also that Observable.just(null) is forbidden: nulls are generally not allowed with RxJava 2.

    Observable<List<User>> searchUsers(final String searchTerm) {
        return Observable.defer(() -> 
             githubUserRestService
                .searchGithubUsers(searchTerm)
                .concatMap(usersList -> 
                     Observable.fromIterable(usersList.getItems())
                       .concatMap(user ->
                           githubUserRestService
                           .getUser(user.getLogin()))
                           .toList()
                           .toObservable()
                       )
                )
                .retryWhen(observable -> 
                    observable.flatMap(o -> {
                        if (o instanceof IOException) {
                            return Observable.just(0);
                        }
                        return Observable.error(o);
                    })
                )
           );
    }