rx.sentMessage(#selector(UIViewController.viewDidLoad)) 没有触发
rx.sentMessage(#selector(UIViewController.viewDidLoad)) is not firing
我有一个应用使用 RxSwift
跟随 MVVM
。
ViewController
import UIKit
import RxSwift
import RxCocoa
final class ProfileViewController: BaseViewController<ProfileView> {
var viewModel: Attachable<ProfileViewModel>!
var bindings: ProfileViewModel.Bindings {
let viewDidLoad = rx.sentMessage(#selector(UIViewController.viewDidLoad))
.mapToVoid()
.asDriverOnErrorJustComplete()
let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear))
.mapToVoid()
.asDriverOnErrorJustComplete()
let viewWillDisappear = rx.sentMessage(#selector(UIViewController.viewWillDisappear))
.mapToVoid()
.asDriverOnErrorJustComplete()
let logout = navigationItem.rightBarButtonItem?.rx.tap
.mapToVoid()
.asDriverOnErrorJustComplete()
return ProfileViewModel.Bindings(
connectStore: viewWillAppear.asDriver(),
disconnectStore: viewWillDisappear.asDriver(),
fetchProfileByUserId: viewDidLoad.asDriver()
)
}
private lazy var disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
private func configureView() {
navigationItem.title = "Your account"
}
}
extension ProfileViewController: ViewModelAttaching {
func bind(viewModel: ProfileViewModel) -> ProfileViewModel {
return viewModel
}
}
ViewModel
import RxSwift
import RxCocoa
import ReSwift
final class ProfileViewModel: ViewModelType {
typealias Dependency = HasReduxStore & HasImagePicker
lazy var userID: String = __user__.id
let data = PublishSubject<ProfileHeaderViewModel>()
let endSession = PublishSubject<Void>()
private let dependency: Dependency
struct Bindings {
let connectStore: Driver<Void>
let disconnectStore: Driver<Void>
let fetchProfileByUserId: Driver<Void>
}
private lazy var disposeBag = DisposeBag()
init(dependency: Dependency, bindings: Bindings) {
self.dependency = dependency
bindings.connectStore
.asObservable()
.map { self }
.bind(onNext: dependency.store.subscribe)
.disposed(by: disposeBag)
bindings.disconnectStore
.asObservable()
.map { self }
.bind(onNext: dependency.store.unsubscribe)
.disposed(by: disposeBag)
bindings.fetchProfileByUserId
.asObservable()
.bind(onNext: dispatchFetchForProfileById)
.disposed(by: disposeBag)
}
private func dispatchFetchForProfileById() {
dependency.store.dispatch(FetchProfileByUserId(payload: userID))
}
}
extension ProfileViewModel: StoreSubscriber {
func newState(state: AppState) {
}
}
当我的视图加载时,我希望 bindings.fetchProfileByUserId
被调用。然而什么也没有发生。我更新了我的绑定以在 viewWillAppear
上触发,一切都按预期工作。
我不知道为什么 viewDidLoad
在这种情况下不起作用。
您可以将 .debug()
添加到您的 viewDidLoad
链,以及将 print() 添加到 viewDidLoad
方法以查看到底发生了什么,但从外观上看,它似乎你在 viewDidLoad
之后设置你的绑定(也许在 configureView
中?)已经被解雇所以你永远不会得到任何额外的事件,这在视图控制器创建期间只发生一次并且会解释为什么 viewDidAppear
有效。
我有一个应用使用 RxSwift
跟随 MVVM
。
ViewController
import UIKit
import RxSwift
import RxCocoa
final class ProfileViewController: BaseViewController<ProfileView> {
var viewModel: Attachable<ProfileViewModel>!
var bindings: ProfileViewModel.Bindings {
let viewDidLoad = rx.sentMessage(#selector(UIViewController.viewDidLoad))
.mapToVoid()
.asDriverOnErrorJustComplete()
let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear))
.mapToVoid()
.asDriverOnErrorJustComplete()
let viewWillDisappear = rx.sentMessage(#selector(UIViewController.viewWillDisappear))
.mapToVoid()
.asDriverOnErrorJustComplete()
let logout = navigationItem.rightBarButtonItem?.rx.tap
.mapToVoid()
.asDriverOnErrorJustComplete()
return ProfileViewModel.Bindings(
connectStore: viewWillAppear.asDriver(),
disconnectStore: viewWillDisappear.asDriver(),
fetchProfileByUserId: viewDidLoad.asDriver()
)
}
private lazy var disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
private func configureView() {
navigationItem.title = "Your account"
}
}
extension ProfileViewController: ViewModelAttaching {
func bind(viewModel: ProfileViewModel) -> ProfileViewModel {
return viewModel
}
}
ViewModel
import RxSwift
import RxCocoa
import ReSwift
final class ProfileViewModel: ViewModelType {
typealias Dependency = HasReduxStore & HasImagePicker
lazy var userID: String = __user__.id
let data = PublishSubject<ProfileHeaderViewModel>()
let endSession = PublishSubject<Void>()
private let dependency: Dependency
struct Bindings {
let connectStore: Driver<Void>
let disconnectStore: Driver<Void>
let fetchProfileByUserId: Driver<Void>
}
private lazy var disposeBag = DisposeBag()
init(dependency: Dependency, bindings: Bindings) {
self.dependency = dependency
bindings.connectStore
.asObservable()
.map { self }
.bind(onNext: dependency.store.subscribe)
.disposed(by: disposeBag)
bindings.disconnectStore
.asObservable()
.map { self }
.bind(onNext: dependency.store.unsubscribe)
.disposed(by: disposeBag)
bindings.fetchProfileByUserId
.asObservable()
.bind(onNext: dispatchFetchForProfileById)
.disposed(by: disposeBag)
}
private func dispatchFetchForProfileById() {
dependency.store.dispatch(FetchProfileByUserId(payload: userID))
}
}
extension ProfileViewModel: StoreSubscriber {
func newState(state: AppState) {
}
}
当我的视图加载时,我希望 bindings.fetchProfileByUserId
被调用。然而什么也没有发生。我更新了我的绑定以在 viewWillAppear
上触发,一切都按预期工作。
我不知道为什么 viewDidLoad
在这种情况下不起作用。
您可以将 .debug()
添加到您的 viewDidLoad
链,以及将 print() 添加到 viewDidLoad
方法以查看到底发生了什么,但从外观上看,它似乎你在 viewDidLoad
之后设置你的绑定(也许在 configureView
中?)已经被解雇所以你永远不会得到任何额外的事件,这在视图控制器创建期间只发生一次并且会解释为什么 viewDidAppear
有效。