2

Everytime we resolve a protocol/class then we get a new object. My coordinator needs a view model and my controller needs the same view model.

internal class LoginFactory: Assembly {
func assemble(container: Container) {
    let delegate = UIApplication.shared.delegate as? AppDelegate
    container.register(LSViewModel.self) { res in
        return LSViewModel()
    }
    container.register(LSCoordinator.self, factory: { res in
        let cord = LSCoordinator(window: (delegate?.window)!)
        cord.viewModel = res.resolve(LSViewModel.self)
        return cord
    })
    container.register(LSViewController.self) { res in
        let cont = StoryboardScene.Login.lsViewController.instantiate()
        cont.viewModel = res.resolve(LSCoordinator.self)?.viewModel
        return cont
    }
}

}

The coordinator goes like

internal final class LSCoordinator: BaseCoordinator<Void>, Coordinator, HasDisposeBag {

private let window: UIWindow
weak var navigationController: UINavigationController!
//LSViewModel implements LSViewModelType
var viewModel: LSViewModelType!
init(window: UIWindow) {
    self.window = window
}

func start() -> Observable<Void> {
    let lslViewController: LSViewController = DependencyManager.getResolver().resolve(LSViewController.self)!
    navigationController = UINavigationController(rootViewController: lsController)

    viewModel.outputs.doneTapped
        .subscribe(onNext: { [weak self] in self?.showLoginTypes() }).disposed(by: disposeBag)
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
    return .never()
}

func showLoginTypes() {
    print(“blah blah”)
}

}

The problem is, when I am trying to inject viewModel in my lsViewController then a different instance of lsViewModel is created. As a result the Rx bindings are not working and the print statement is not executed. Is there any way to pass the same view model to both coordinator and controller?

Ped7g
  • 16,236
  • 3
  • 26
  • 63
A_G
  • 2,260
  • 3
  • 23
  • 56

0 Answers0