具有最后状态的RxJava Observable

时间:2016-04-07 09:57:34

标签: java rx-java observable frp rx-swift

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

在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)
你能帮帮我吗?感谢

1 个答案:

答案 0 :(得分:4)

BehaviorSubject 有一个 value()方法,该方法返回当前值

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