Let's spend a little bit of time today to summarize what MVVM is used for and also a few of its major advantages. This architecture pattern is very powerful once you understand the implementation and workflows that you must adopt. Make sure to leave questions or comments down below if you have any.
Comments (5)
st4n
4 years ago
hi Brian :)
quick question:
shouldn't all the handlePan logic/functionality be moved outside the View (to vm perhaps or controller rather)?
I wanted to ask this long before but waited to mvvm part but nothings changed in this topic.. even in "regular" mvc, the view shouldn't be responsible for anything except for how it looks (or am I wrong?).
Brian Voong
4 years ago
st4n
4 years ago
sorry for being such pain in the a&% sometimes :)
my question is purposeful. I have received several reviews with similar comments.
And each time i needed to find some way around.
So.. how to extract/transform this "messy" part into something more elegant and "by the book"?
I (probably there is more of I's) would be super happy if you could focus more on this kind of "harder/formal" topics.
Brian Voong
4 years ago
FranckNdame
4 years ago
Hi Brian, I had the same problem as @st4N and I didn't want to put the handlePan logic in the controller so I migrated it to the view model.
I am now using a protocol like this:
protocol CardViewDelegate {
func didPan(view: UIView, gesture: UIPanGestureRecognizer)
}
to enable the View and Viewmodel to communicate. It works perfectly fine. I am wondering if this could actually be a solution as I haven't seen anyone user protocols on a model yet. Thanks
Bashir SK
4 years ago
This is my first time learning MVVM, i though it was some scary design pattern but Brian has really explained it very well. I just started my first job as junior iOS dev, 2 months in now and what i've noticed the codebases look very messy with view controllers having 1000s of lines of code. I saw one tableView delegate method that has almost 60 lines of code.
Brian Voong
4 years ago
Christophe Bugnon
4 years ago
What about creating an Internally injection in our CardView as you made generally:
var cardViewModel: CardViewModel! {
didSet {
imageView.image = UIImage(named: cardViewModel.imageName)
informationLabel.attributedText = cardViewModel.attributedString
}
}
Or same create a Dependency injection like that:
convenience init(cardViewModel: CardViewModel) {
self.init(frame: .zero)
setupViews(with: cardViewModel)
cardGesture()
}
It removes required init and reduces our code in our HomeController at 7 lines for managing our cardView, and if we want to test it, it's easier.
Maybe you'll make it after, I ask it now because I'm just here right now. ^^
Cinquain
4 years ago
I definitely prefer the MVVM model over MVC. I would only use MVC for very simple applications.
alokach123
3 years ago
Hey Brian,
Obviously I cannot watch this entire thing in one go, I was wondering if you could make a feature where it saves our place, and next time we jump on to your website to start the course up again, it will take us to that. Thank you