Swift 5:如何获取BehaviorRelay的值并绑定RxSwift和MVVM
Swift 5: How to get the value of a BehaviorRelay and binding with RxSwift and MVVM
我正在尝试从视图模型中的服务获取金额值,然后在 ViewController 中将其绑定到 amountLabel。
这是我的视图模型:
class AmountViewModel {
private let accountService: AccountManagerProtocol
let _amount = BehaviorRelay<Int>(value: 0)
let amount: Observable<Int>
private let disposeBag = DisposeBag()
init(accountService: AccountManagerProtocol = AccountManager()) {
self.accountService = accountService
amount = _amount.asObservable()
getAmount()
}
func getAmount(){
accountService.getAccount()
.map{ [=11=].amount ?? 0 }
.bind(to: _amount)
.disposed(by: disposeBag)
}
}
这是我的 ViewController,
我做了这样的事情,获取viewModel的金额,但我觉得这不是最好的方法,我想获取金额的值并能够以更简单的方式将其绑定到amountLabel。
private extension AmountViewController {
private func bindViewModel() {
amountView.titleLabel.text = viewModel.title
//Get Amount
viewModel.amount
.observe(on: MainScheduler.instance)
.withUnretained(self)
.subscribe(onNext: { owner, amount in
if let amountString = amount.currencyToString() {
owner.inputAmountView.amountLabel.text = "BALANCE: \(amountString)"
}
})
.disposed(by: disposeBag)
}
这是最明显的简化:
class AmountViewModel {
let amount: Observable<Int>
init(accountService: AccountManagerProtocol = AccountManager()) {
amount = accountService.getAccount()
.map { [=10=].amount ?? 0 }
}
}
private extension AmountViewController {
private func bindViewModel() {
viewModel.amount
.compactMap { [=10=].currencyToString().map { "BALANCE: \([=10=])"} }
.observe(on: MainScheduler.instance)
.bind(to: inputAmountView.amountLabel.rx.text)
.disposed(by: disposeBag)
}
}
但我想我会把 compactMap
闭包中的代码移到视图模型中...
我正在尝试从视图模型中的服务获取金额值,然后在 ViewController 中将其绑定到 amountLabel。
这是我的视图模型:
class AmountViewModel {
private let accountService: AccountManagerProtocol
let _amount = BehaviorRelay<Int>(value: 0)
let amount: Observable<Int>
private let disposeBag = DisposeBag()
init(accountService: AccountManagerProtocol = AccountManager()) {
self.accountService = accountService
amount = _amount.asObservable()
getAmount()
}
func getAmount(){
accountService.getAccount()
.map{ [=11=].amount ?? 0 }
.bind(to: _amount)
.disposed(by: disposeBag)
}
}
这是我的 ViewController, 我做了这样的事情,获取viewModel的金额,但我觉得这不是最好的方法,我想获取金额的值并能够以更简单的方式将其绑定到amountLabel。
private extension AmountViewController {
private func bindViewModel() {
amountView.titleLabel.text = viewModel.title
//Get Amount
viewModel.amount
.observe(on: MainScheduler.instance)
.withUnretained(self)
.subscribe(onNext: { owner, amount in
if let amountString = amount.currencyToString() {
owner.inputAmountView.amountLabel.text = "BALANCE: \(amountString)"
}
})
.disposed(by: disposeBag)
}
这是最明显的简化:
class AmountViewModel {
let amount: Observable<Int>
init(accountService: AccountManagerProtocol = AccountManager()) {
amount = accountService.getAccount()
.map { [=10=].amount ?? 0 }
}
}
private extension AmountViewController {
private func bindViewModel() {
viewModel.amount
.compactMap { [=10=].currencyToString().map { "BALANCE: \([=10=])"} }
.observe(on: MainScheduler.instance)
.bind(to: inputAmountView.amountLabel.rx.text)
.disposed(by: disposeBag)
}
}
但我想我会把 compactMap
闭包中的代码移到视图模型中...