我创建了一个每20秒运行一次的Observable。 我需要在我使用onPreExecute方法的AsyncTask中每轮之前显示进度栏(UI组件)。 如何在RXJava2中做到这一点?
disposables = Observable
.interval(0, 20, TimeUnit.SECONDS)
.doOnNext(n -> MainCulc_actions())
.subscribeOn(Schedulers.io()) // Run on a background thread
.observeOn(AndroidSchedulers.mainThread()) // Be notified on the main thread
.subscribeWith(new DisposableObserver<Long>() {
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
@Override
public void onNext(Long res) {
UpdateTheUI();
avi.smoothToHide();
}
});
答案 0 :(得分:0)
您可以尝试doOnSubscribe
。例如
disposables = Observable
.interval(0, 20, TimeUnit.SECONDS)
.doOnNext(n -> MainCulc_actions())
.subscribeOn(Schedulers.io()) // Run on a background thread
.observeOn(AndroidSchedulers.mainThread()) // Be notified on the main thread
.doOnSubscribe(s -> avi.showUI)
.subscribeWith(new DisposableObserver<Long>() {
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
@Override
public void onNext(Long res) {
UpdateTheUI();
avi.smoothToHide();
}
});