RXJava how to make an observable continue onError

710 Views Asked by At

Consider the following example :

    WidgetObservable.text(searchView).debounce(250, TimeUnit.MILLISECONDS).flatMap(new Func1<OnTextChangeEvent, Observable<List<String>>>() {

        @Override
        public Observable<List<String>> call(OnTextChangeEvent onTextChangeEvent) {
            String s = onTextChangeEvent.text().toString();
            return provider.getGeocodeObservable(s, 5).flatMap(new Func1<List<Address>, Observable<String>>() {
                @Override
                public Observable<String> call(List<Address> addresses) {
                    return Observable.from(addresses).map(new Func1<Address, String>() {
                        @Override
                        public String call(Address address) {
                            String addresss = address.getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                            String city = address.getLocality();
                            //String state = address.getAdminArea();
                            String country = address.getCountryName();
                            return String.format("%s, %s, %s", addresss, city, country);
                        }
                    });
                }
            }).collect(new Func0<List<String>>() {
                @Override
                public List<String> call() {
                    return new ArrayList<String>();
                }
            }, new Action2<List<String>, String>() {
                @Override
                public void call(List<String> strings, String s) {
                    strings.add(s);
                }
            }).subscribeOn(Schedulers.io());
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<String>>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.e("MapsActivity", "What?", e);
        }

        @Override
        public void onNext(List<String> strings) {
            Log.d("MapsActivity", "works " + strings);
            adapter.clear();
            adapter.addAll(strings);
            adapter.notifyDataSetChanged();
            searchView.showDropDown();
        }
    });

Here i observe text changes on a auto complete view, and act upon it to get relevant Address suggestions using Android-ReactiveLocation. its working nicely untill an error occures at the the geocode observable which causes onError to be invoked on the final subscriber. Ever since this error occurs the WidgetObservable wont emit events anymore.

How do i fix it so that onError will not cause the Observable to stop emitting text change events so that whole flow continues to happen?

BTW, Im new to Reactive programming and RXJava so any improvement suggestions to my code would be more then welcome:)

1

There are 1 best solutions below

0
On BEST ANSWER

You could try the OnErrorReturn operator on the geocode observable i.e.

provider.getGeocodeObservable(s, 5)
        .onErrorReturn(new Func1<Throwable, List<String>>() {
                @Override
                public String call(Throwable throwable) {
                    return  Arrays.asList(new String[]{});
                }
            })