Setting visibility of ProgressBar before and after emitting value from an observable

Muhammad Arsal

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.

yosriz

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.

edited at
0

Comments

0 comments
Login to comment

Related

Rx Java: Sleep before OnNext (Sleep before emitting from Observable)

Return observable before emitting event

Observable only emitting first value

Group in ConstraintLayout prevents setting ProgressBar visibility

RxJS - Observable is not emitting value with .next() on first call

RxJs Observable - Handle 404 by emitting default value

RxJS: Setting default value for observable from another observable

How to make one Observable sequence wait for another to complete before emitting?

Visibility of ProgressBar

Cannot control Progressbar visibility from onActivityResult in Fragment

Does an observable keep emitting values after observer has unsubscribed?

Start emitting after another observable emitted its first item

Observable that continues emitting events after having emitted an error

Rxjs: Observable with takeUntil(timer) keeps emitting after the timer has ticked

How can I add progressbar before the load data from firebase and dismiss this progressbar after loaded data from firebase with firebasercycler adapter

Wrap an Observable. Do something before and after each emitted value

RxJS - switchMap not emitting value if the input observable is empty array

Regenerate session id after or before setting a secure value

Emitting value from VUE 3 setup() listener

Best way to ensure subscription happened before emitting next value

Identify emitting observable in RxJava

Observable not emitting any values

SwiftUI: Setting View visibility based on a property value?

setState not setting initial value from api before page load

RxJava: prevent an observable from emitting until data from another observable is emitted

angularjs progressbar value from scope

Setting a Polygon Visibility from TextBlock style

Check the visibility of the indeterminate ProgressBar

Ngrx - selector is not emitting new value after store change