如何从ViewController
观察ViewModel
类内Variable<...>
值(RxSwift
Variable
(的变化
因此,如果我在ViewModel
中的任何Variable<..>
的值在ViewModel
中发生的事情中发生变化,那么ViewController
将被注意到"嘿!ViewModel中的一个或多个变量<..>发生了变化!向ViewModel询问更新UI和更新UI所需的数据!">
然后ViewController
调用ViewController内的方法updateUI()
,并在其中向ViewModel
询问所有信息,如状态/状态,以更新UI,如:
func updateUI() {
progressBar.hide = viewModel.getProgressBarVisibility()
errorMessageLabel.hide = viewModel.getErrorMessageVisibility()
errorMessageLabel.text = viewModel.getErrorMessageText()
.....
...
}
如果单个ViewModel的属性值发生更改,为什么要更新完整的UI
RxSwift使您能够独立地听取更改,并可以相应地做出反应/更改UI。
在我看来,ViewModel和ViewController类应该是这样的:
class ViewModel {
private var progressBarVisibiity:Variable<Double> = Variable.init(0.0)
private var errorMessageVisibiity:Variable<Double> = Variable.init(0.0)
private var errorMessageLabel:Variable<String> = Variable.init("Default text")
public func setProgressBarVisibiity(_ value:Double) {
progressBarVisibiity.value = value
}
public func setErrorMessageVisibiity(_ value:Double) {
errorMessageVisibiity.value = value
}
public func setErrorMessageLabel(_ value:String) {
errorMessageLabel.value = value
}
public func observeProgressBarVisibiity() -> Observable<Double> {
return progressBarVisibiity.asObservable().observeOn(MainScheduler())
}
public func observeErrorMessageVisibiity() -> Observable<Double> {
return errorMessageVisibiity.asObservable().observeOn(MainScheduler())
}
public func observeErrorMessageLabel() -> Observable<String> {
return errorMessageLabel.asObservable().observeOn(MainScheduler())
}
}
class ViewController {
let viewModel = ViewModel()
let disposeBag = DisposeBag()
func observeViewModelChanges() {
viewModel
.observeProgressBarVisibiity()
.subscribe(onNext: { value in
self.progressBar.hide = viewModel.getProgressBarVisibility()
})
.disposed(by: disposeBag)
viewModel
.observeErrorMessageVisibiity()
.subscribe(onNext: { value in
self.errorMessageLabel.hide = value
})
.disposed(by: disposeBag)
viewModel
.observeErrorMessageLabel()
.subscribe(onNext: { value in
self.errorMessageLabel.text = value
})
.disposed(by: disposeBag)
}
}
要更新UI,我建议使用viewState
变量,您可以在需要时在视图模型类中更新该变量,例如:
/// Making it generic allow you to add your view specific state
public enum ViewState<T> {
// add all the case you need
case loading
case ready(T)
case failure(Error)
}
然后在您的视图中模型类:
let viewState: Variable<ViewState<YourViewControllerState>> = Variable<ViewState<YourViewControllerState>>(.loading)
YourViewControllerState是一个包含特定情况的枚举:
enum YourViewControllerState {
case progressBarShowed, //...
}
最后在您的ViewController中:
viewModel.viewState
.asObservable()
.observeOn(MainScheduler.instance)
.subscribe { [weak self] _ in
self?.updateUI()
}.disposed(by: disposeBag)
private func updateUI() {
guard isViewLoaded else {
return
}
switch viewModel.viewState.value {
case .ready(.progressBarShowed):
progressBar.hide = viewModel.getProgressBarVisibility()
case .failure:
errorMessageLabel.hide = viewModel.getErrorMessageVisibility()
errorMessageLabel.text = viewModel.getErrorMessageText()
}
}
我们可以构造一个双向绑定运算符,您只需使用bindTo即可。以下是ControlProperty<->的实现变量和变量<->变量:
infix operator <->
@discardableResult func <-><T>(property: ControlProperty<T>, variable: BehaviorSubject<T>) -> Disposable {
let variableToProperty = variable.asObservable()
.bind(to: property)
let propertyToVariable = property
.subscribe(
onNext: { variable.onNext($0) },
onCompleted: { variableToProperty.dispose() }
)
return Disposables.create(variableToProperty, propertyToVariable)
}
你可以在下面的帖子中找到你问题的详细答案。RxSwift 中的双向绑定