'Retry the same request which has just failed due to error in RXJava with Kotlin

Problem statement: Whenever an error occurs for the request, I am showing snackbar in the UI which has a RETRY button on it. When clicking on retry button same request should be made. I have tried various things but no success, this is one code snippet which I have tried just now.

In Presenter:

private val _retryClickObserver: PublishSubject<Unit> = PublishSubject.create()

@VisibleForTesting
val retryClickObserver: PublishSubject<Unit> = _retryClickObserver 

fun loadData() {
        addSubscription(repository.getData()
            .doOnSubscribe { view.showProgressBar() }
            .doAfterTerminate { view.hideProgressBar() }
            .retryWhen { observable ->
                observable.zipWith(itemClickObserver.toFlowable(BackpressureStrategy.LATEST),
                    BiFunction<Throwable, Any, Any> { _: Throwable, Any -> Any
                    })
            }
            .subscribe({ handleResult(it) }, { handleError(it) }
            )
        )
    }

In Fragment:

override fun showErrorMessage() {
        showSnackBar(rootLayout, R.string.try_again) {           
                presenter.retryClickObserver.onNext(Unit)
            }

        }
    }

What can I try next?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source