You can check this method. Idea: create a wrapper, observable, then add 2 actions "start" and "finish".
public static <T> Observable<T> bindLoadingAction(final Runnable start, final Runnable finish, final Observable<T> source) { final Observable<T> o = source.observeOn(AndroidSchedulers.mainThread()); return o.lift(new Observable.Operator<T, T>() { @Override public Subscriber<? super T> call(final Subscriber<? super T> child) { return new Subscriber<T>() { @Override public void onStart() { super.onStart(); if (start != null) { new Handler(Looper.getMainLooper()).post(start::run); } child.onStart(); } @Override public void onCompleted() { if (finish != null) { finish.run(); } child.onCompleted(); } @Override public void onError(Throwable e) { if (finish != null) { finish.run(); } child.onError(e); } @Override public void onNext(T t) { if (finish != null) { finish.run(); } child.onNext(t); } }; } }); }
Using:
bindLoadingAction(this::showLoadingView, this::hideLoadingView, YourRetrofitObservable);
(I used the Java 8 syntax. This :: showLoadingView means a reference to the showLoadingView method in the current class.)
source share