sergdort / CleanArchitectureRxSwift

Example of Clean Architecture of iOS app using RxSwift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible wrong use of MVVM pattern

stefanomondino opened this issue · comments

Hi guys,

first of all, let me thank you for your work, which is really great and inspiring!
I've been using MVVM in iOS since 4 years now and I've learned that everyone has its personal way to implement things with this pattern and, most of all, everyone intend a "View Model" in different way.
However, there seems to be 2 things that almost everyone agrees upon:

  1. Views should NEVER know the existence of the "model layer"
  2. In terms of separations between layers, UIViews and UIViewControllers falls inside the "View" layer

Taking a look at your example, in the PostsViewController, you're fetching the UITableView with the .rx extension: inside the closure, you're directly referencing the UITableViewCells IBOutlets and directly populating with model properties, which (in my opinion) totally breaks the MVVM pattern for different reasons :

  • The UITableViewCell (a view) is fully aware (and directly accessing) something from the Model layer.
  • The cell is configured by a single view controller, meaning that the same cell must be configured in every scenario it's going to be used (suppose you have another screen where you would like to present
  • There's no "cell view Model" concept, which could be useful to reuse the same cell (with same outlets) to display data from different models.

Does it make sense to you? Am I being too "strict to the pattern" for your scenario?
Thanks in advance :)

Hi, @stefanomondino

Thanks for rising this issue :)

As a reference to MVVM I like to use msdn post.

I would agree here that we are doing data formatting on a binding layer is wrong, effectively leaking the Post entity to UI. The reason I did this is that there is no formatting involved in current example, e.g we set titleLabel.text = post.title.

But I think you are right and we should be precise! As some people out there could be confused (a specially people new to iOS development), thinking this the right way to do it.

I'll try to find some time and address it. Or if you want to do it yourself I would be more then happy to see your approach.

I've forked your repo, I'll try to work something out. I'll let you know!

@sergdort here it is. It's really a simple approach, in a real project i usually create a little bit more of structure inside the view model layer.
You can take a look (if interested) at Boomerang, it's something that solves this kind of problem by separating view model types ("List view models, Item view models, Selectable view models, etc.). We're writing docs in these days, then we'll release v1.

I guess we can close this one :)

Feel free to reopen if you think there are thing that can be improved