1

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);
                }));
    }
Sam
  • 6,215
  • 9
  • 71
  • 90

2 Answers2

2

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);
                })
            )
       );
}
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • toList().toObservable() fixed the issue,but adding Observable.just(0) giving another error, "bad return type in lambda expression: Observable cannot be converted to ObservableSource extends Integer>" – Sam Sep 25 '17 at 12:45
  • Either change it to `just((Object)0)` or `.error(o)` – akarnokd Sep 25 '17 at 12:58
  • Thanks lot for the explanation, if you can back this line with some reference link would be great for anyone landing on this question."Note also that Observable.just(null) is forbidden: nulls are generally not allowed with RxJava 2." – Sam Sep 25 '17 at 13:36
  • It is on the [wiki about differences between the two versions](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#nulls). – akarnokd Sep 25 '17 at 16:23
0

Try to use RxJava2CallAdapterFactory:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build();
shmakova
  • 6,076
  • 3
  • 28
  • 44
  • but this compile error coming in Rxjava2 when combining with concatMap Operator – Sam Sep 25 '17 at 11:58