Why I haven’t seen your channel before. Top quality content. Looking forward to learn topics coordinators, architecture, unit tests, rxswift and anything you make :)
As few others commented, there is a slight disagreement on the use of VM. The primary purpose of all view patterns are for separating presentation logic away from business logic. So VM as the name suggests should contain only fields and logic specific to a view or set of views. The fields in VM should be subset of actual Model and logic in VM is mainly presentation oriented like formatting a date for the view or responding the change of a field in the view. The core business logic (that could include using network calls to achieve that) and full set of fields should be part of the Model.
IMO Networking can be called in the viewmodel, but the viewmodel shouldn't have actual implementation of networking. It should call some abstract services which have the networking injected. External services can fetch the model itself. Then, like you said, all the viewmodel does is convert the model to data that the view can render. So the viewmodel doesn't technically contain the networking either, and neither should the model... since the abstract service itself might be fetching the model.
Awesome explanation brother 👏👏. You please keep posting all videos related to Swift iOS . I have great hope that many of us get benefitted from your explanation and questions you ask in every step🙂. We need a gem like you in every learning curves😊
Really nice video with a lot of good points that other videos on the topic missed. I do have a few issues with it. Calling MV* patterns architecture seems wrong to me. They are patterns designed to solve a specific issue. They are not themself the architecture of the app, seeing them as architecture is what causes people to try to cram all code into one of the letters of the acronym which is not the point with the patterns, it's is also what lead to strange bastard patterns like MVC-N and MVVM-C. MV* patterns are a great tool and they do participate inf the architecture but, they are not THE architecture. Also, you talk like 100% test coverage in itself is a goal, while you should strive for good coverage, the coverage itself is not a way to measure quality as the tests cold be testing absolutely nothing.
This! I think this is why it's so hard to bring architecture as a topic on companies, because all the tutorials or learning content treats MVP or mvvm as architecture, so something like Clean Architecture becomes something that seems a theoretical ideal and people usually don't see what the point even is! Also it seems this video doesn't fully showcase dependency inversion, as it's using singletons left and right and not showing how to fully take advantage of the pattern! One could also argue that the view should not ask for the VM for data but just use the published properties, but here there's a bunch of exposed methods
As per MVVM view should not be aware about model however when you explained about protocol the closure, it was returning array of flight object to the View in this case view is dependent on model. I was explaining the exactly what you mentioned in one of the Apple interview however interviewer told me as per MVVM view should not be aware about model. What is your thought on it?
Great video as always. One question I was having is that what's the use case for the model to communicate back to the ViewModel? I understand that View Model is responsible for say making the network call and update the model but didn't come up with a use case where the model changes (without VM's involvement) and needs to communicate back to the VM.
At 9:50 you talk about the folder structure. As an alternative I suggest keeping the 3 folders Views, viewModels and Models, while keeping the folder structure of the viewModels folder flat. Any hierarchy will be found in the Views folder and the corresponding view model for a view can be found easily. I suggest defining each view model in an extension of it's view. What do you think?
Excellent content, wish we had your videos earlier but nevertheless its a brilliant content.. keep on publishing videos.. Best Wishes !!! I had a query, Dependancy Injection - With help of Sw-Inject how it can be used & implemented ? Would love to have a video on the same. Many Thanks !!!!
Some blogs are saying we can create the view model as an extension of content view in SwiftUI and create a @State variable of view model in content view to receive the viewmodel updates. Is it a good practice ?
Can we use "Color" in ViewModel, because it needs SwiftUI to be imported in ViewModel. I have some logic related to color to be implemented in ViewModel. How can I do it without using SwiftUI in ViewModel?
Thanks for another great video dude! You mentioned, we only keep business logic in VM. How about some other app logic such as date components, calculator formula etc? I know you also mentioned those should be a separated class but how do we call them, from VM or VC?
Those utility methods can be extensions of their classes (eg. Date), or static functions may be, so that you can directly call them without instance. Some people prefer having a singleton (say Helper) or global functions for these stuff. If you want to have an instance, you can have it injected through some method.
May you please keep xcode version compatible with 12.4 (12D4e) in next videos, there is some people still cannot upgrade to latest version of OS/Xcode.
Hi Pallav Thank you for the wonderful videos. I have one question why did you make the protocol as type of any object? Just to use weak? If this is the case then how a struct will implement that protocol? and in case if implement then how it can be reference type? 🤯
Protocal can be use any type. By deafult they are Any. To restrict the use struct and enum. We should use anyobject. Anyobject is reference type. Then any reference make stronge recycle count to make the. weak.
Hi Great Tutorial, I have a confusion in UIKit when using MVVM and dependency injection while sending data to DetailViewController. Can you please provide me any code snippet for that. Or share link if you have any tutorial on it. Thanks in advance!
We can test network calls by writing stubs. By creating fake URLSession, overriding resume method of URLSessionDataTask, and returning dummy response from it. I'll try covering it. Thanks for the suggestion 🙂
Hi i have a question,can the viewmodel layer be reusable for multiple view layer? Or everytime you create a new view controller you always have to create the new viewmodel even though it have the same use case with the existing viewmodel file
The whole point of SwiftUI is that it's declarative and driven by state. You shouldn't be using delegates. Also why are you creating box listeners when you're using combine?