How to implement search feature with SearchView, Retrofit and RxJava (RxBindings)?
<p>When the  user types into the <code>SearchView</code> widget, the app should make an
API call (<strong>in background thread</strong>) to fetch search results from server, and display them (<strong>in UI thread</strong>) in RecyclerView.</p>

<p>I use the following code in my fragment:
</p>

<pre class="lang-java prettyprint-override"><code>@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.my_fragment, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

    RxSearchView.queryTextChanges(searchView)
                .debounce(400, TimeUnit.MILLISECONDS)
                .map(CharSequence::toString)
                .switchMap(query -&gt; retrofitService.search(query))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber&lt;List&lt;Item&gt;&gt;() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(LOG_TAG, "Error", e);
                    }

                    @Override
                    public void onNext(List&lt;Item&gt; items) {
                        // adapter.addItems(...)
                    }
                });
}
</code></pre>

<p>But I get an exception:</p>

<pre class="lang-java prettyprint-override"><code>java.lang.IllegalStateException: Must be called from the main thread. Was: Thread[RxIoScheduler-2,5,main]
at com.jakewharton.rxbinding.internal.Preconditions.checkUiThread(Preconditions.java:35)
at com.jakewharton.rxbinding.support.v7.widget.SearchViewQueryTextChangesOnSubscribe.call(SearchViewQueryTextChangesOnSubscribe.java:18)
at com.jakewharton.rxbinding.support.v7.widget.SearchViewQueryTextChangesOnSubscribe.call(SearchViewQueryTextChangesOnSubscribe.java:10)
...
</code></pre>

<p>When I remove <code>.subscribeOn(Schedulers.io())</code>, the search API call is fired when fragment is created and no query is typed in <code>SearchView</code> and I get exeption</p>

<pre class="lang-java prettyprint-override"><code>retrofit2.adapter.rxjava.HttpException: HTTP 422 
</code></pre>

<p>then, when I type my search query <code>retrofitService.search(query)</code> is no longer called.</p>