Rxjava / Android monitors the progress of multiple subscribers triggered at different times
I'm looking for a way to use rxjava to maintain consistency to monitor the progress of multiple subscribers that may be triggered at different times. I know how to merge or merge all users after triggering at the same time from one method, but I don't know which method will be used when triggering them from different methods at different times
For example, if I have 2 long-running tasks attached to the button press, I press button 1 and trigger the observable / user. Halfway through the run, I press button 2 to trigger the second observer / user
I want to enable buttons when no tasks are running and disable buttons when one or more tasks are running
Is that possible? I try to avoid setting instance variable flags as well
resolvent:
I will use a separate behaviorsubject and scan it to monitor the execution status. This is very similar to the instance variable, but it may inspire you to find a better solution. Like this:
private final BehaviorSubject<Integer> mProgressSubject = BehaviorSubject.create(0);
public Observable<String> firstLongRunningOperations() {
return Observable.just("First")
.doOnSubscribe(() -> mProgressSubject.onNext(1))
.finallyDo(() -> mProgressSubject.onNext(-1)));
}
public Observable<String> secondLongRunningOperations() {
return Observable.just("Second")
.doOnSubscribe(() -> mProgressSubject.onNext(1))
.finallyDo(() -> mProgressSubject.onNext(-1));
}
public Observable<Boolean> isOperationInProgress() {
return mProgressSubject.asObservable()
.scan((sum, item) -> sum + item)
.map(sum -> sum > 0);
}
The usage is as follows:
isOperationInProgress()
.subscribe(inProgress -> {
if (inProgress) {
//disable controls
} else {
//enable controls
}
});
Using this method, you can run for a long time without firing them all. Just don't forget to call doonsubscribe and finallydo
PS. sorry, I didn't test it, but it should be OK