I've an RxJava observable in my android application which emits items brought from server and repeats like this:
Observable.interval(60, TimeUnit.SECONDS)
.flatMap(new Func1<Long, Observable<Something>>() {
@Override
public Observable<Something> call(Long aLong) {
showProgressBar(true);
return dataManager.getSomething();
}
}).repeat().retry().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Something>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Something something) {
// Do something
showProgressBar(false);
}
});
I want to set visibility of my ProgressBar
to VISIBLE
when the call to the server is being made using dataManager.getSomething()
and hide it after getting the results. Hiding works perfect but where should I call my showProgressBar(true)
method to make it visible again.
You can do it in many ways:
Add donOnNext()
after the interval.
add doOnSubscribe()
at the dataManager.getSomething()
also putting showProgressBar(true)
at the flatMap()
operator should also do the trick.
You just need to make sure to it will happen on the mainThread()
as your changing the UI, you can achieve it with the observerOn(AndroidSchedulers.mainThread())
operator before your operator, just don't forget to change it back afterwards if you need it on a different thread.
Actually, In this case, assuming you creating this observable in main thread, you will call the showProgressBar(true)
at main thread, as in default the notifications will happen at the thread created the Observable
, that means that the dataManager.getSomething()
will also happen at main thread, so you should add subscribeOn()
to a different thread.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments