@jrg.developer Hi Joshua, In MVVM video, I donât see where you place the code for networking. Where should it be? in MVVM?
@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]
In some projects, I have seen developers put networking directly code into the ViewModel (that is, using NSURLSession
, etc directly in the ViewModel).
Personally, however, I donât like this because it violates the Single Responsibility Principle. That is, the view model is doing at least two things: view transformations (thatâs what view models do, after all) and networking.
Instead, I propose that Model-View-Controller-Networking (MVC-N) is a better pattern for networking. Wherein, you pull out networking code into itâs own ânetworking clientâ class.
I actually cover this pattern in this video series:
https://videos.raywenderlich.com/courses/72-ios-design-patterns/lessons/3
Once you have a separate ânetworking clientâ class, you can then choose where its appropriate to put calls to the networking client. This ultimately depends on your projectâs specific use-case, but most likely, youâll wind up putting this in either the View Controller or View Model.
There really isnât a âbest choiceâ on which to do here⊠Rather, you should think about why you want to put calls to networking in one versus the other and run it through each of the SOLID principles. If it doesnât break any SOLID principles, youâre probably okay. If it does break a SOLID principle, you should consider what the long-term consequences will be, and if youâre okay with them or not.
For example, if you break âSingle Responsibilityâ, will this lead to a huge class later on? Will it result in you duplicating code later on? Etc.
If youâd like to learn more about SOLID, I highly recommend this book:
Clean Coding by Robert Martin
This video can not be played back. Iâve waited more than 10 minutes, but Iâm still buffering. Iâm in Seoul, South Korea. My internet speed is so fast but It is not be played⊠Please check.
@yoshiboarder It actually works for me after all, so please go ahead and try to play it again when you get a chance. Thank you!