How to add retryWhen() for limited time (Eg 3 time retries only) getting IOException

a 夏天 提交于 2019-12-11 16:07:16

问题


I am fetching response from server using 3 web API calls, but in case of getting IOException i have to retry those calls for 3 times using retryWhen().
How can I achieve that?

i have tried adding below code, but not getting expected output

retryWhen(new Function<io.reactivex.Observable<Throwable>, ObservableSource<?>>() {
                    int retryCount = 0;

                    @Override
                    public ObservableSource<?> apply(io.reactivex.Observable<Throwable> errors) throws Exception {

                        return errors.flatMap(new Function<Throwable, ObservableSource<?>>() {
                            @Override
                            public ObservableSource<?> apply(Throwable throwable) throws Exception {
                                retryCount++;
                                if (retryCount < 4) {
                                    Log.e(TAG, " Exception retrying = "+retryCount );
                                    return io.reactivex.Observable.just("");
                                }
                                return io.reactivex.Observable.error(throwable);
                            }
                        });
                    }
                })

public void onClickLogin(View view) {

             io.reactivex.Observable
             .zip(getLogin(Constants.EMAILID, Constants.PASSWORD),
             getUserInfo(Constants.EMAILID, Constants.PASSWORD),
            getProductDetails(Constants.EMAILID, Constants.PASSWORD).subscribeOn(Schedulers.io()),
                                .observeOn(AndroidSchedulers.mainThread())
                        new Function3<List<LoginModule>,
                                List<UserInfoModule>, ProductModule, AllZipData>() {
                            @Override
                            public AllZipData apply(List<LoginModule> loginModuleList, List<UserInfoModule> useerInfoModules, ProductModule productModule) throws Exception {

                                AllZipData allZipData = new AllZipData();
                                allZipData.setLoginModuleList(loginModuleList);
                                allZipData.setUserInfoModuleList(UserInfoModule);
                                allZipData.setProductModule(productModule);

                                return allZipData;
                            }
                        }).subscribe(new Observer<AllZipData>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }

            @Override
            public void onNext(AllZipData allZipData) {


                MyDatabase MyDatabase = MyDatabase.getInstance(context);

                for (int i = 0; i < allZipData.getUserInfoModuleList().size(); i++) {

                    UserInfoTable userInfoTable = new UserInfoTable();
                    userInfoTable.setValue1(allZipData.getUserInfoModuleList().get(i).getValue1());
                    userDatabase.userDao().insertUserInfo(userInfoTable);
                }

            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "onError: all zip data " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.e(TAG, "onComplete: all data zipped");
            }
        });
}

回答1:


There's a version of retry that calls a predicate to determine if the stream should be subscribed or not - if you should retry. The predicate receives 2 arguments - number of attempts and the throwable. It seems to be what you want. I'd try:

observableThatMightError
   .retry((count, throwable) -> throwable instanceof IOExceptio && count <= 3)

this will retry if the throwable is an IOException and you haven't retried yet for at least 3 times.



来源:https://stackoverflow.com/questions/57429527/how-to-add-retrywhen-for-limited-time-eg-3-time-retries-only-getting-ioexcep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!