RxJava Observable具有最后状态

前端之家收集整理的这篇文章主要介绍了RxJava Observable具有最后状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从Rx Java开始,我想创建一个可以保存最后状态的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)

你能帮我吗?谢谢

解决方法

BehaviorSubject有一个value()方法,它返回它的当前值
final BehaviorSubject<Boolean> subject = BehaviorSubject.<Boolean>create(false);
final Boolean value = subject.getValue();
原文链接:https://www.f2er.com/java/129426.html

猜你在找的Java相关文章