简体   繁体   中英

Where does the coordinator go in a RxSwift + MVVM + Coordinator situation if coordinators aren't in RxSwift?

I'll keep this as short as possible: we started the app full RxSwift but our coordinators started to get overwhelmed to maintain and extend so we are reworking them to not use RxSwift and now we can't figure out how to use them and where to keep them.

Simplest example: view controller with 1 button, view model, coordinator. Where do I call coordinator.navigate(to: .mySection) and how? Should the coordinator be kept in the view controller? I thought they should be kept in the view model but subscribing in the view model is not the recommended usage.

I don't want to over-engineer this but I hope to get some insightful help.

If you want to use the coordinator as a dependency of your ViewModels, check out this library: Action .

Action lets you bind the action from a button with the ViewModel. You just define an action in your ViewModel, and bind it in the view. The Action type is just a sequence that admits other sequence as input and returns another sequence as output. So you don't need to use a DisposeBag on your ViewModel.

If you want to see examples, checkout the Action's GitHub project or, if you want to see in app examples, the Ray Wenderlich RxSwift book has a pretty neat example on how to integrate MVVM and Coordinators.

None of the MVVM elements directly address Coordinators so if you are using the benefits of Soroush's Coordinator pattern, you are probably using VIPER but you may not be aware of this, which is not really important.

In VIPER, the R stands for Routing which perfectly fits with the definition of Coordinators.

Two things I really like to suggest are:

  1. Most of the turorials on the Internet teach you to create a seperate coordinator for each of your UIViewController s BUT I personally asked soroush about this and he replied : it is not the best practice with coordinators. So I used only two coordinators in my latest App and quite happy with the result.
  2. If you would like to spend some time on VIPER, I suggest to assume your ViewControllers are the Presentor in your VIPER model; I have seen some VIPER tutorials on the Internet expressing otherwise.

PS MVVM is great but it is not scalable, I humbly suggest to take a look at VIPER. Here is a good article that you may find usefull.

我建议使用XCoordinator及其建议的体系结构MVVM-C,我已经在2个大型/复杂应用程序中使用过,这很容易。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM