RXJava如何使一个observable继续onError

时间:2016-05-22 19:57:40

标签: android rx-java reactive-programming observable android-reactivelocation

考虑以下示例:

    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();
        }
    });

在这里,我观察自动完成视图上的文本更改,并使用Android-ReactiveLocation对其进行操作以获取相关的地址建议。它很好地工作,直到在geocode observable上发生错误,导致在最终订阅者上调用onError。自从发生此错误以来,WidgetObservable将不再发出事件。

如何修复它以便onError不会导致Observable停止发出文本更改事件以便整个流程继续发生?

顺便说一句,我是Reactive编程和RXJava的新手,所以对我的代码的任何改进建议都会受到欢迎:)

1 个答案:

答案 0 :(得分:3)

您可以尝试使用可观察地理编码的OnErrorReturn运算符,即

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