RxJava可观察到最后一个状态

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

在RxSwift中,这将是变量(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();
链接地址: http://www.djcxy.com/p/91295.html

上一篇: RxJava Observable with last state

下一篇: How to handle permission requests outside Activity and Fragment?