Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava Observable with last state

I’m starting with RxJava, and I would like to create an observable that can save the last state…

In RxSwift, that would be Variable (https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#variables), but I can’t found an equivalente in RxJava…

I found a work around but it’s a lot of boiler plate code

private boolean isGettingCompanies = false;

public boolean isGettingCompanies() {
    return isGettingCompanies;
}

private void setIsGettingCompanies(boolean isGettingCompanies) {
    this.isGettingCompanies = isGettingCompanies;
    isGettingCompaniesPublishSubject.onNext(isGettingCompanies);
}

private final PublishSubject<Boolean> isGettingCompaniesPublishSubject = PublishSubject.create();

public Observable<Boolean> isGettingCompaniesPublishSubject() {
    return isGettingCompaniesPublishSubject.asObservable();
}

the RxSwift equivalent is this

private(set) var isGettingCompanies: Variable = Variable(false)

Can you help me please? Thanks

like image 211
Bruno Coelho Avatar asked Apr 07 '16 09:04

Bruno Coelho


1 Answers

BehaviorSubject has a value() method which returns it's current value

final BehaviorSubject<Boolean> subject = BehaviorSubject.<Boolean>create(false);
final Boolean value = subject.getValue();
like image 161
krp Avatar answered Sep 24 '22 07:09

krp