我正在尝试使用concat运算符来返回来自领域的缓存数据和来自api调用的数据。我遇到一个问题,当api调用observable抛出异常它shorcut-cut进程并返回onError。我通过从此链接https://stackoverflow.com/a/32489728/6284609调用onErrorResumeNext找到了解决方案。现在我有另一个问题是我无法检查异常类型,我想检查它以便调用连接错误消息以及与我的api调用相关的其他自定义异常。欢迎任何解决方案。一般情况下,我想从缓存和api调用中读取数据,如果存在连接错误,我希望从缓存中获取数据,并且我希望api observable显示错误异常。谢谢。
这是我的函数加载缓存数据和api调用
public static Observable<List<WorkoutWeekSessions>>
fetPackageWorkOutWeeksAndSessions(int traineeId, Realm realm, final Context context) {
List<WorkoutWeekSessions> list = new ArrayList(realm.where(WorkoutWeekSessions.class).findAllAsync());
Observable<List<WorkoutWeekSessions>> cachedWorkoutSessions = Observable.just(list)
.subscribeOn(Schedulers.computation());
WorkOutService requestService = ServiceGenerator.createService(WorkOutService.class);
Observable<List<WorkoutWeekSessions>> apiWorkoutSessions= requestService.getPackageWorkOutWeeksAndSession(UrlManager.getTraineePackageWoroutsSessionsUrl(),
traineeId)
.flatMap(new Function<JsonElement, Observable<List<WorkoutWeekSessions>>>() {
@Override
public Observable<List<WorkoutWeekSessions>> apply(JsonElement jsonElement) throws Exception {
JsonObject asJsonObject = jsonElement.getAsJsonObject();
String result = asJsonObject.get(UrlManager.ResultTypes.RESULT).getAsString();
Timber.d(TAG, "result Tag" + result);
if (UrlManager.ResultTypes.isError(result) || UrlManager.ResultTypes.isFailure(result)) {
String errorMessage = asJsonObject.get(UrlManager.ResultTypes.RESULT_ERROR_MESSAGE).getAsString();
return Observable.error(new Exception(errorMessage));
}
if (UrlManager.ResultTypes.isSucess(result)) {
if (!GsonHelper.isNull(asJsonObject.get(UrlManager.ResultTypes.RESULT_DATA))
&& asJsonObject.get(UrlManager.ResultTypes.RESULT_DATA).isJsonArray()) {
List<WorkoutWeekSessions> workoutWeekSessions = WorkoutWeekSessions
.PackageWorkOutWeeksListParser
.fromJsonElement(asJsonObject
.getAsJsonArray(UrlManager
.ResultTypes
.RESULT_DATA));
return Observable.just(workoutWeekSessions);
} else {
return Observable.error(new Exception("Data is empty"));
}
}
if (UrlManager.ResultTypes.isLogin(result)) {
return Observable.error(new SessionTimeoutException());
}
return Observable.error(new Exception("Unkown Tag"));
}
})
.subscribeOn(Schedulers.io());
return Observable
.concat(cachedWorkoutSessions, apiWorkoutSessions
.onErrorResumeNext(new Observable<List<WorkoutWeekSessions>>() {
@Override
protected void subscribeActual(Observer<? super List<WorkoutWeekSessions>> observer) {
}
}))
.observeOn(AndroidSchedulers.mainThread());
}
这是我如何调用它并用于在使用concat之前处理异常
private void retrievePackageWorkOutWeeksAndSessions() {
if (!swipRefreshLayout_reLoad.isRefreshing()) {
swipRefreshLayout_reLoad.setRefreshing(true);
}
WorkOuttNetworkCall.fetPackageWorkOutWeeksAndSessions(1, realmUI,getContext())
.subscribe(new Observer<List<WorkoutWeekSessions>>() {
@Override
public void onSubscribe(Disposable d) {
Timber.d(TAG, "onSubscribe() called with: d = [" + d + "]");
compositeSubscription.add(d);
}
@Override
public void onNext(List<WorkoutWeekSessions> list) {
Timber.d(TAG, "onNext() called with: value = [" + list.size() + "]");
swipRefreshLayout_reLoad.setRefreshing(false);
if (list.size() == 0) {
Toast.makeText(getContext(), R.string.noDietPackageAvailable, Toast.LENGTH_SHORT).show();
}
bindRecyclerData(list);
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
Timber.d(TAG, "onError() called with: e = [" + e + "]");
swipRefreshLayout_reLoad.setRefreshing(false);
if (e instanceof IOException) {
Toast.makeText(getContext(), R.string.connectionError, Toast.LENGTH_SHORT).show();
} else if (e instanceof NullPointerException) {
} else if (e instanceof SessionTimeoutException) {
AuthenticationManager.logOut();
} else {
Toast.makeText(getContext(),
e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete() called");
}
});
}