RXJava如何使一个可观察的继续onError
RXJava how to make an observable continue onError
考虑以下示例:
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 获得相关的地址建议。它工作得很好,直到在地理编码可观察到的错误发生之前,导致在最终订阅者上调用 onError。自从发生此错误后,WidgetObservable 将不再发出事件。
我该如何修复它,以便 onError 不会导致 Observable 停止发出文本更改事件,从而使整个流程继续发生?
顺便说一句,我是 Reactive 编程和 RXJava 的新手,因此欢迎对我的代码提出任何改进建议:)
您可以在地理编码可观察对象上尝试 OnErrorReturn
运算符,即
provider.getGeocodeObservable(s, 5)
.onErrorReturn(new Func1<Throwable, List<String>>() {
@Override
public String call(Throwable throwable) {
return Arrays.asList(new String[]{});
}
})
考虑以下示例:
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 获得相关的地址建议。它工作得很好,直到在地理编码可观察到的错误发生之前,导致在最终订阅者上调用 onError。自从发生此错误后,WidgetObservable 将不再发出事件。
我该如何修复它,以便 onError 不会导致 Observable 停止发出文本更改事件,从而使整个流程继续发生?
顺便说一句,我是 Reactive 编程和 RXJava 的新手,因此欢迎对我的代码提出任何改进建议:)
您可以在地理编码可观察对象上尝试 OnErrorReturn
运算符,即
provider.getGeocodeObservable(s, 5)
.onErrorReturn(new Func1<Throwable, List<String>>() {
@Override
public String call(Throwable throwable) {
return Arrays.asList(new String[]{});
}
})