0

Hope you guys are doing well, I have been working on a personal Android Project using RxJava and Retrofit. It's a search request for GitHub issues, I am getting input from the user when he clicks Search Button and using a PublishSubject object to emit the search text.

button.setOnClickListener(view -> {
    publishSubject.onNext(editText.getText().toString());
});

and I am mapping this emit to an Observable using retrofit like this

publishSubject.concatMap(dataModel::getIssues)
              .subscribeOn(Schedulers.computation())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(this::loadData, this::onError);

public Observable<List<Issue>> getIssues(String queryText) {
    String[] query_params = queryText.split("/");
    return gitHubApiService.getIssues(query_params[0], query_params[1], "open");
}

In result I am expecting List of Issues

public void loadData(List<Issue> issues) {
    mProgressDialog.setVisibility(View.INVISIBLE);

    if( issues.size() == 0) {
        noIssueText.setVisibility(View.VISIBLE);
    } else {
        mRecyclerView.setVisibility(View.VISIBLE);
        mIssuesList.clear();
        mIssuesList.addAll(issues);
        mAdapter.notifyDataSetChanged();
    }
}

But my code seems to have some implementation issue Since it never emits anything from the network, not even on error is called. I have tested the same example with the Observable I get from Retrofit API, so there is no retrofit error and so I think there is some problem with my concatMap logic. Any help will be much appreciated

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
savvisingh
  • 93
  • 7
  • `.subscribeOn(Schedulers.computation())` you should be subscribing on `io`, as you are doing io, there – njzk2 May 19 '17 at 18:02
  • tried that but no success, but solved it adding scheduleOn in getIssues method like the answer below – savvisingh May 19 '17 at 18:12

1 Answers1

2

On first parse, I think that you might be making the network call in the main thread. Have you tried the following?

public Observable<List<Issue>> getIssues(String queryText) {
  String[] query_params = queryText.split("/");
  return gitHubApiService.getIssues(query_params[0], query_params[1], "open")
       .subscribeOn(Schedulers.io());
}

Thing is, your onClickListener callback runs on the main thread, and there's no other context switch in the observable pipeline.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • Hey Thanks that worked, but I am not sure why can't it run on the Main Thread. It should display the results as well. – savvisingh May 19 '17 at 18:09
  • `gitHubApiService` is, I assume a Retrofit2 client. In Retrofit + Rx, the OkHttp thread pools / callback pools are not used, instead the more flexible Scheduler system of Rx is applied. This means that unless you explicitly use [a scheduler](https://github.com/square/retrofit/blob/master/retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/RxJava2CallAdapterFactory.java#L80) when creating the client, all the calls will *not* switch a scheduler by default. – Tassos Bassoukos May 19 '17 at 18:12
  • ok I got the point, but I am also specifying the scheduler after concatMap operation, but why my Observable didn't used that. – savvisingh May 19 '17 at 18:18
  • The `subscribeOn` applies only once - at the original `subscribe` that starts the chain (BTW, computation?). That scheduler stops being used as soon as you subscribe to the `PublishSubject`. – Tassos Bassoukos May 19 '17 at 18:22