我从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();