Combining Observed and Modified RxTextView

As an example, to start working with RxAndroid, I am trying to implement a search query that launches a call to rest when users insert something.

So far I have two working parts. First observation on 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 calls the REST API using the 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 using the operator flatMapas follows:

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 the 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 this by calling subscribeOn(AndroidSchedulers.mainThread(), but in this case, of course, I get a NetworkOnMainThread exception.

So how do I do this? What is the correct way to combine different observable data that must run on different threads?

+4
1

.observeOn(AndroidSchedulers.mainThread()).

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)
+3

Source: https://habr.com/ru/post/1628700/


All Articles