I am bit new to app development in SwiftUI. In last 2-3 months I have developed few small iOS app just to learn concepts. Otherwise I am experienced developer in C#, Java etc.
One thing I am little unsure about SwiftUI is the right set of architecture around developing classes for the project. I mean like there could be couple of ways
We write model classes that represent the data that our application need to hold. In purist form the model classes should only be concerned about just data, I mean the model classes should only hold the attributes that reflect the data.
Say I am writing a Inventory management app so if one of the model is Merchandise item then its attribute could be Id, name, price, barcode etc. In my opinion the model classes should not concerned about View's concern like #Published, #ObservableObject, #State, #EnvironmentObject etc etc. Model should just stick to representing domain only. Correct?
Ideally the model classes should be written as Class not Struct (if I carry over my understanding of OOPS from C++ to Java/C# where we write classes not struct)
The second set of classes we need are views i.e. inherited from View. Undoubtedly these has to be Struct as SwiftUI framework works this way.
Now in between the model classes (presuming they are classes or at max we made them Struct for the sake of SwiftUI framework) and View classes, lots of communication, state change, eventing has to happen to make app worthy of doing something. I mean handling user gestures, creating & editing data which should update screens when user navigate back and forth between UIs.
I found it little hard to connect Model (if they are developed in purist form) and View. Hard in the sense that as I started writing Views and look to implement use cases which involve data editing, reflecting changes in views etc I found pure model classes insufficient. They have to be modified to reflect SwiftUI features like binding, observability, published to sync data between view and model and also between two models.
Wondering what is the right design pattern to connect and communicate between model and view? Is MVVM the right design pattern to be used in SwiftUI based project? If not then what else is right pattern?
If MVVM is right pattern then are there any quality guide or resources or sample SwiftUI projects (gitHub??) I can look at and learn?
I think your hypothesis is wrong.
Ideally the model classes should be written as Class not Struct
Make a distinction between model with and without state.
Model without state should be value type.
Model with state should be reference type.
The second set of classes we need are views i.e. inherited from View. Undoubtedly these has to be Struct as SwiftUI framework works this way.
Value type cannot be inherited. I also think they are models rather than views.
E.g.; struct Model: View {}
They are models, that can be used to build views, hence the View conformance.
The fact that it is of value type also supports that it's not view. (cannot be inherited)
Whether your model has state or not, as long as it conforms to View, it can be used to render a view. This is the design of SwiftUI, taking a page from React, IMO. (I personally don't see React ever bothering with MVVM)
Now since you think Model should be strictly without state, you have a problem.
You need a reference type object that handles all the state and bridge it to the view.
You can follow MVVM. In that case you lose most of the SDK support that builds around struct Model: View, e.g.; #State, #StateObject, #EnvironmentObject, and associated automatic binding and safety checks. You would also add at least one reference type for each of the views, and put most of the control / business logic in it. (where else can you put it?)
So it's in effect a view controller. And you would be doing most of the Control work on a reference type object, without safe guards of immutability. That is on top of all the overheads you would introduce; and since you would pass view model around to be "reusable", you are going to have all the usual bugs caused by shared reference with implicit states.
Take my above assessment with a grain of salt, of course.
Or you can do what official guide do: struct Model: View {} with possible states in it.
The key here, as SDK requires you to put extra annotation, is that state objects are explicitly identified. E.g.; #StateObject, #ObservableObject. And you have to create reference type objects separate from your model. It essentially cuts out the middle man that is view model.
I think this is the most difficult thing for MVVM devs to get used to. You no longer need a view model to do what a view model does.
But shouldn't it be obvious? View model depends on how binding is designed. If SwiftUI has a binding design so efficient, (it does so declaratively without extra object) view model could be automatic.
SwiftUI removes UIViewController, does that mean you can't have Control?
I don't see MVC developers come out and create a Controller for every view.
Same reasoning, you can have MVVM without VM. Design pattern is a #State of mind.
To summarize:
I think the best architecture is official SDK. Learn to walk before you run.
MVVM in legacy sense is an option too. But I think you need to consider the possibility that
traditional wisdom is not up-to-date with latest SDK.
Related
I'm having a hard time wrapping my head around MVVM as I'm in the progress of learning SwiftUI development with Swift for iOS. After reading various tutorials and watching YouTube videos, this is my understanding:
View: The UI
Model: Data layer
ViewModel: "Glue" between view and model. Contains business logic.
However, after doing additional research, I found this article which talks about the role of controllers in MVVM. Now, this confuses me a lot because my understanding was that the ViewModel in MVVM already takes care of what a controller would do e.g. in MVC and therefore controllers would not exist in apps based on MVVM.
Question: Do controllers exist in apps based on MVVM or is the article simply wrong?
It is merely a question of terminology. This author’s choice of terminology is a bit unusual, and I would not get hung up on that. As you dive into these architectural patterns, you’re going to see all sorts of different uses of terminology. (Usually it is the term “view model” that is subject to some creative interpretation.)
Regarding this author’s particular use of the term “controller”, earlier in the document, when talking about view controllers, he starts referring to them as simply “controllers”. This is imprecise, but it is the terminology that he adopts throughout the article.
(As an aside, I would avoid referring to view controllers just as “controllers”, because for many of us, the term “controller” refers to something far more abstract, unrelated to the UI. E.g., see the discussion of “controllers” late in Dave Delong’s excellent video A Better MVC. But let’s set all of that aside and talk about the role of view controllers in MVVM.)
If you are employing MVVM in UIKit or AppKit, then you have a model, a view, and a view model. But you still have view controllers, too. So what happens to them? They do not disappear (unless you switch to something like SwiftUI).
Most/many of us simply consider the view controller as part of the broader “view” layer. A view controller is a UIKit object. It configures views. It is not some abstract object, but rather is an integral part of the UI. In MVVM, it often bears responsibility for hooking up the views to the view model, etc. Regardless of what architectural pattern you adopt, the hallmark of a good view controller implementation is that it is almost entirely restricted to UI related tasks.
So, in short, in that article you reference, the author is merely saying that view controllers are still in the mix (for non-SwiftUI projects, at least). He decided to describe it as a separate layer in his MVVM diagram. Many of us just consider the view controllers to just be part of the “view” layer. It is a question of terminology.
Im learning about SwiftUI for macOS development, and Im wondering what would be the right approach when creating Managers, services etc. These are objects not related to the UI - strictly business logic things, not even ViewModels. Should I not use things like ObservableObject, Published, AppStorage, etc. in this kind of classes? On one hand it seems to be beneficial to add this, so that I can later easily use them with ViewModels, and bind directly to some properties. On the other hand it seems wrong - like these property wrappers are strictly SwiftUI related. So should I resign from these things in managers, services and other business logic objects?
Both ObservableObject and Published are defined in Combine, so they are fine to use outside of the UI layer of your app. On the contrary, #AppStorage is defined in SwiftUI, so you should only use it in Views.
However, the fact that something is defined in Combine doesn't necessarily mean that it provides meaningful value to use when not binding to your UI.
ObservableObject's main use case is storing objects as #ObservedObject on a View and getting automatic view updates. Even though you could consume the objectWillChange Publisher that ObservableObject provides from outside a view, there's not much use for it, so in general, I wouldn't advice on making objects conform to ObservableObject unless they are directly updating your SwiftUI views.
#Published on the other hand provides significant value even outside your ViewModel-View interaction. Observing changes to mutable state is quite useful even in a pure business logic layer, such as exposing the current location of the user in a location manager class and observing it from a view model. Marking this property #Published makes it much easier to observe.
The separation of what objects a View should directly access is a subjective topic, so I won't go into that in much detail, but if you want to keep your View independent of your business logic objects other than the ViewModel, you should store managers, services, etc on your ViewModel and proxy any properties that need to update the UI through the ViewModel. For this to work, you don't need to make your managers/services conform to ObservableObject, however, storing mutable state as #Published can be quite helpful.
ObservableObject allows you to store state in an object that is independent from any views. When you conform your class to ObservableObject then views can observe it (or listen). To broadcast (publish) values of your variables to the views, just mark them with property wrapper #Published. Obviously, you know that.
I consider this pattern as strictly dedicated to Object->Views communication.
I know this is a bit of an ethereal question, but I'm working on an app that takes its entire structure from a remote JSON object and I'm trying to figure out the best way to approach the creation of Views, View Controllers, and Models. Currently, I am using RestKit to grab, parse, and map objects. I will have the structure of the views, controls, and fields defined at runtime, so how should I dynamically create and manage object composition, view controllers, and all appropriate delegates? (I imagine KVC and KVO will have a huge part in all of this.)
A few things to address:
Nested/chained delegation to allow leaf control events to bubble up to the root controller and then down to the proper model.
Dynamic object composition to allow a view to contain an arbitrary combination of subviews at runtime
Dynamically linking data between views and the proper model objects
Are there any good resources, guides, or examples of implementing/planning for this type of architecture?
This answer may not satisfy you but i will try to help.
Initialize dictionaries(NSDictionary) and detecet objects&values with [yourObject objectForKey:#"yourJSONKey"]; and [yourKey valueForKey:#"yourJSONValue"]; and after this create&push your views so that thay play a rol in run time(according to the JSON response)
Recently I've been reading up on the MVC pattern and wish to apply it to my iPhone development. However, there seem to be so many variations of the pattern that I'm not sure exactly how it should be applied.
As far as I gather, the view will notify the controller of any actions which have been performed and the controller will in turn update the data model (if required). The data model will notify the view whenever a change to the data occurs and the view then updates it's display of the data appropriately.
In this basic model, the controller only has knowledge of the data model. However, I can't seem to figure out how to employ this design within my iPhone app.
The following page suggests an alternative version of the pattern where the controller has an awareness of both the data model and the view and all communication between the model and view is performed via the controller. It also seems to suggest that the model and view have access to the controller. Would I be right in suggesting that the data model interacts with the controller via some form of notification (notifications or KVO) and that the view interacts with the controller via actions?
Is this second model correct?
http://www.bogotobogo.com/DesignPatterns/mvc_model_view_controller_pattern.html
Many thanks,
Danny
I found Paul Hegartys explanation on MVC in iOS very helpful. see his Stanford iTunes U video. MVC starts at minute 22.
edit
The link of the video doesn't bring you there as expected. it is 1. Introduction to Cocoa Touch, Objective-C, Tools, and MVC (September 21, 2010)
MVC has been around for a long time so there are many variations (or misquotes) to the pattern. Although, the concepts are much the same for most MVC implementations I have seen.
I would focus on how Apple defines MVC. Which can be found in the Cocoa Design Patterns guide and from sample code downloaded from the SDK site (MVCNetworking example).
With iOS you will often will have Models and ViewControllers(which are a merged role of both the controller and the view).
Also, Martin Fowler has some great MVC stuff in his GUI Architectures.
iOS development is very much orientated towards the MVC pattern.
It is usually done with viewControllers and a model. The view is build in Interface Builder, assigned to the controller and the model part is retrieved from elsewhere.
I would say that for Cocoa-Touch the second "version" of the pattern is the one that best describes what usually goes on.
The idea behind MVC is that the model and the view is reusable, but the controller is often fitted to the problem at hand.
This is also true for iOS development, especially if you use interface builder.
The view is hooked up to the viewController via actions/delegates and the model either broadcast its changes through KVO notification or by the controller pulling new data.
There is tons of code available from Apples developer portal and you should start out by looking at some of that code. Having your eyes and mind tuned to looking for the MVC pattern you will see they use it constantly, with the delegate pattern on top to provide event better abstraction
In my opinion, second one is better. Model and view should be separated completely. If view receives notification from model, the view will depends on design of model. By placing controller here, tightly coupled circular-dependency created.
Finally, each part cannot be developed independently, divide-and-conquer strategy is just impossible to use.
My advise for general cases:
Make view and model passive and independent as much as possible. Major mutation must be done with only external manipulation. It should not be changed actively.
Make controller actively controls both of them and other controllers.
In iOS, a UIView is a view which is fully passive. In most cases, all major mutation always done externally by UViewController. And model part should be implemented yourself completely as you want. (Or you can integrate models into controller if it's small enough, however, I don't recommend it)
In some big featured UIView, a sub-scale MVC patterns are used. Fractal!
Sticking to Apple's guidelines, I create one subclass of UIViewController per screen of my iPhone application. However I consistently find that these classes become very large, both in terms of sheer lines of code and number of member variables.
By definition they end up being responsible for quite a number of different concerns (e.g. view life cycle, mediating between views and models, sometimes touch handling, control logic, managing alerts and other UI state).
Not only does this violate the Single Responsibility Principle but it also results in large swathes of code which are near impossible to unit test.
What responsibilities/concerns do you tend to divide off into new classes? What kinds of responsibilities do you think make good candidates for clean separation in the case of UIViewController subclasses?
This is an interesting question and I also struggle on how to properly separate responsibility. It all depends on the context but since testing subclasses of UIVieController can be a pain I try to move as much as I can into model classes. In the spirit of Skinny Controller, Fat Model.
When it comes to tables I have created a base model class for handling all the table view stuff, basically encapsulating what you get when you create a new Navigation Based Core Data project. The in the controller I just forwards the calls to my table model.
I'm trying to keep the methods of the controller as small as possible. Depending on the context I may have several model classes, each responsible for a specific part.
I have also looked into the possibility of using controller factories for getting detail controllers for certain data models.
UIViewController *detailController = [self.controllerFactory controllerForItem:item];
[self presentModalViewController:detailController animated:YES];
This way I can unit test that I get the proper controller for a specific data item without having to involve the parent controller.
With mine, I'm creating categories, abstract parents and using a variety of patterns I've learned in the Java world to reduce complexity and code duplication. But when it comes down to it, I still have one view controller per screen because every screen has at least one thing on it thats unique in some way. There just might not be much code left in the controllers thanks to the infrastructure I've placed around them.