具有最后状态的 RxJava Observable

RxJava Observable with last state

我从 RxJava 开始,我想创建一个可以保存最后状态的可观察对象......

在 RxSwift 中,这将是 Variable (https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#variables),但我在 RxJava 中找不到等价物......

我找到了解决方法,但其中有很多样板代码

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();
}

RxSwift 等价物是这个

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

你能帮帮我吗?谢谢

BehaviorSubject 有一个 value() 方法,returns 它是当前值

final BehaviorSubject<Boolean> subject = BehaviorSubject.<Boolean>create(false);
final Boolean value = subject.getValue();