Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine RxTextView Observable and Retrofit Observable

As an example to getting started with RxAndroid I'm trying to implement a searchbox which triggers a rest call when the users inserts something.

So far I have two working parts. The first observing the EditTextView ...

RxTextView.textChangeEvents(searchEditText)
    .debounce(400, TimeUnit.MILLISECONDS)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<TextViewTextChangeEvent>() {
            @Override
            public void onCompleted() {
                Timber.d("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Timber.e(e, "onError");
                }

            @Override
            public void onNext(TextViewTextChangeEvent e) {
                Timber.d("onNext" + e.text().toString());
            }
        });

... and the second part calling the REST API by using a Retrofit Service:

APIManager.getService().searchRestaurants("test")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<List<Restaurant>>() {
            @Override
            public void onCompleted() {
                Timber.d("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Timber.e(e, "onError");
            }

            @Override
            public void onNext(List<Restaurant> restaurants) {
                Timber.d("onNext");
                for (Restaurant restaurant : restaurants) {
                    Timber.d(restaurant.getId() + ": " + restaurant.getName());
                }
            }
        });

My Problem is combining the two parts. I tried by using the flatMap Operator as following:

RxTextView.textChangeEvents(searchEditText)
        .debounce(400, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap(new Func1<TextViewTextChangeEvent, Observable<List<Restaurant>>>() {
            @Override
            public Observable<List<Restaurant>> call(TextViewTextChangeEvent txtChangeEvt) {
                return APIManager.getService().searchRestaurants(txtChangeEvt.text().toString());
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<List<Restaurant>>() {
            @Override
            public void onCompleted() {
                Timber.d("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Timber.e(e, "onError");
            }

            @Override
            public void onNext(List<Restaurant> restaurants) {
                Timber.d("onNext");
                for (Restaurant restaurant : restaurants) {
                    Timber.d(restaurant.getId() + ": " + restaurant.getName());
                }
            }
        });

When I do this I get following exception:

java.lang.IllegalStateException: Must be called from the main thread. Was: Thread[RxCachedThreadScheduler-1,5,main]
                                                                              at com.jakewharton.rxbinding.internal.Preconditions.checkUiThread(Preconditions.java:28)
                                                                              at com.jakewharton.rxbinding.widget.TextViewTextChangeEventOnSubscribe.call(TextViewTextChangeEventOnSubscribe.java:21)
                                                                              at com.jakewharton.rxbinding.widget.TextViewTextChangeEventOnSubscribe.call(TextViewTextChangeEventOnSubscribe.java:12)

So I tried to fix that by calling subscribeOn(AndroidSchedulers.mainThread() but in this case, of course, I get an NetworkOnMainThread Exception.

So how Do I do this? What is a proper way to combine different Observables which should execute on different Threads?

like image 900
Sven Avatar asked Oct 19 '22 15:10

Sven


1 Answers

Just remove the first .observeOn(AndroidSchedulers.mainThread()). Take a look at this example

Observable.just(1) // 1 will be emited in the IO thread pool
    .subscribeOn(Schedulers.io())
    .flatMap(...) // will be in the IO thread pool
    .observeOn(Schedulers.computation())
    .flatMap(...) // will be executed in the computation thread pool
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(); // will be executed in the Android main thread (if you're running your code on Android)
like image 90
MyDogTom Avatar answered Oct 21 '22 05:10

MyDogTom