Passing List to RxJava Concat and when remove item in onNext or onError causes ConcurrentModificationException

好久不见. 提交于 2021-02-15 07:42:15

问题


I have this code which I can use with concat but what I need is a way of passing Iterator so that I can add or remove items which isn't possible with this code.

I want to be able to remove and add items to concat without causing java.util.ConcurrentModificationException I need to do this for different purpose such as if some items are success remove them and retry others. Or if token is invalid then remove all and refresh token and add all with modified token.

Observable userObservable = getUserObservable(token);
Observable userObservable = getMerchantObservable(token);

List<Observable<?>> observables = Arrays.asList(userObservable, merchantObservable);

Observable.concat(observables)
        .doOnNext(
                result -> observables.remove(0)
                //then if token refreshed call is success then
                //observables.clear()
                //observables.add(updatedUserObservable)
                //observables.add(updateMerchantObservable)
        )
        //.doOnError(error -> System.out.println(error.getMessage()))
        .doOnError(error -> {
            System.out.println(error.getMessage());
            if(((HttpException) error).code() == 401){
                observables.clear();
                observables.add(getRefreshTokenObservable());
            }
        })
        .retryWhen(new RetryWithDelay(5, 2000))
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                result -> System.out.println(result),
                error -> System.out.println("onError called!"));

来源:https://stackoverflow.com/questions/61352701/passing-list-to-rxjava-concat-and-when-remove-item-in-onnext-or-onerror-causes-c

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