MVVM in iOS: who is the responsible for the assembly? - ios

I'm trying to apply MVVM architecture in my iOS project and I've read some articles like this one where it is said that:
(...) we can make the app-wide Router service which will be responsible for performing assembly and the View-to-View presentation
But I don't find any code sample for this kind of class, I'd really appreciate that somebody could explain the appropriate way to implement such "Router" class for a MVVM assembly, or say where I could find an example.

MVVM typically does not use a Router while Clean Architecture applications like VIPER or Clean Swift do.
In MVVMyou instantiate the root UIViewController in your AppDelegate, than set up the ViewModel and assign it to the view controllers viewModel property. Later on, one possible place to configure the upcomming MVVM scene in prepareForSegue.
This is a good explanation of MVVM, which also links to an example project.
You can certainly add those concepts to MVVM. If you want to do so, you can have a look in this Clean Swift example project, adapt the implementation concepts of Router and Configurator and develop your own, customized MVVM approach.
Your question(s) Who is responsible for setting up my MVVM scene and who is responsible for routing in-between scenes is very good though, as you are pinpointing one of the weaknesses of MVVM: Undefined responsibilities in this regards.
The second major weakness is bidirectional communication between ViewModel and View.
Bidirectional communication is also an issue in VIPER (View <-> Presenter and Presenter <-> Interactor).
The only iOS specific approach (I am aware of) which handles all of the mentioned issues is Clean Swift. I stop writing here as it gets off topic.

Related

iOS. How to not put the same code into three ViewControllers that do almost the same

I'm trying to create a really simple Concentration game and got a question.
I have a ViewController where users can pick difficulty level(easy, normal hard). Then, there are 3 more ViewControllers, each ViewController is a new level. The game logic works pretty much the same, only some minor things should be changed. I could just copy and paste the same code into all three controllers and that doesn't seem to be a good idea.
Can I avoid this somehow? Maybe I don't even need 3 ViewControllers, but this is my first app ever and my knowledge is miserable.
Create one enum in common viewController class and pass that enum value according to your requirement and you can vary the code according to that enum value in common viewController
enum GameLevel {
case easy, normal, hard
}
You can create protocols and use those in view controllers.
https://medium.com/#agoiabeladeyemi/protocol-in-swift-with-practical-examples-8b955268ce39
I think you choose protocol oriented programming here, Now Apple prefers Protocol oriented programming in the development.
Tips:
You can create a protocol with the required methods which can be used in different controllers.
You can create separate Modal for the data which is common in all 3 view controllers.
Even I suggest you to go with some architecture like MVVM/VIP + coordinator which can be useful over here(This is optional if you are a beginner)
Benefits:
Your code will be managed.
You will learn a very handy concept of protocol oriented programming.
If you have a time and you want to explore different architecture then it is really good to go with MVVM.

implementation of mvvm architecture in objective-c

How to implement the mvvm architecture in objective-c ? I need sample project to learn the mvvm architecture and also i need to know that what is needed to give in model, view, viewModel and how to separate in objective-c ? what is the difference between the normal project and mvvm project ?
Jesto I can give you details about what you ask briefly.Even if it is explanation you can easily understand what I post here because it has sources with examples and you can directly try the source.
First the Model
The model layer is not as self-explanatory as it may seem.
As you would expect, it will have your model objects, potentially
covering most of the layer surface. In the tickets example, you would
have a Ticket struct that would live in your model.
I find the following components to also be part of the model layer:
Network Code. The shape should be something like this. Ideally, you’d only use a single class for network communication across your
entire app.
Persistence Code. You would implement this with Core Data or simply by saving an NSData blob directly to disk.
Parsing Code. Any objects that parse network responses and the like should be included in the Model layer as well.
While the model objects and the parser are domain-specific, the
network code will be highly reusable.
The controller will then use all the elements in your model layer to
define the flow of information in your app.
Second the View
When a user interacts with your app, they are interacting with the
view layer. The view is considered the “dumb” part of your app, since
it shouldn’t contain any business logic. In code terms, you’ll
normally see:
UIView subclasses. These range from a basic UIView to complex custom UI controls.
A UIViewController (arguably). Since a UIViewController is strongly coupled with its own root UIView and its different cycles
(loadView, viewDidLoad), I personally consider it to be part of this
layer, but not everyone agrees.
Animations and UIViewController transitions.
Classes that are part of UIKit/AppKit, Core Animation and Core Graphics.
Typical code smells found in this layer manifest in different ways,
but boil down to including anything unrelated to UI in your view
layer. A classic code smell is making a network call from a
UIViewController.
It’s tempting to put a bunch of code in your UIViewController and be
done with it, so you can meet that deadline. Don’t do it! In the short
term, you might save a couple of minutes, but in the long term, you
could lose hours looking for a bug, or have trouble when you want to
reuse code inside one view controller in another.
Third MVVM
Model-View-ViewModel, or MVVM, is an MVC derivation. Conceptually,
it’s similar. The biggest difference is in the communication between
layers, and instead of a controller, you use a view model.
In practice, MVVM shines when it has an FRP framework to support it.
Since the model is now observed by the view model and the view model
by the view, the FRP paradigm becomes a natural choice to manage the
information flow. This leads to a better separation between layers,
which translates in decoupled components that are easy to test.
Bottom line: architecture is important, but in my opinion the right
programming paradigm will influence more the overall quality of your
code. It’s also important to note, that most often that not, you will
have in the same app different approaches. This includes both
architecture and paradigm. You might think this will disrupt
consistency inside the codebase, but you should always use the right
tool for the job.
Finally MVC
The Model-View-Controller (MVC) design pattern assigns objects in an
application one of three roles: model, view, or controller. The
pattern defines not only the roles objects play in the application, it
defines the way objects communicate with each other. Each of the three
types of objects is separated from the others by abstract boundaries
and communicates with objects of the other types across those
boundaries. The collection of objects of a certain MVC type in an
application is sometimes referred to as a layer—for example, model
layer.
MVC is central to a good design for a Cocoa application. The benefits
of adopting this pattern are numerous. Many objects in these
applications tend to be more reusable, and their interfaces tend to be
better defined. Applications having an MVC design are also more easily
extensible than other applications. Moreover, many Cocoa technologies
and architectures are based on MVC and require that your custom
objects play one of the MVC roles.
Difference between Normal and MVVM
Models — responsible for the domain data or a data access layer
which manipulates the data, think of ‘Person’ or ‘PersonDataProvider’
classes.
Views — responsible for the presentation layer (GUI), for iOS environment think of everything starting with ‘UI’ prefix.
Controller/Presenter/ViewModel — the glue or the mediator between the Model and the View, in general responsible for altering the Model
by reacting to the user’s actions performed on the View and updating
the View with changes from the Model.
MVVM Says
The latest and the greatest of the MV(X) kind The MVVM is the newest
of MV(X) kind thus, let’s hope it emerged taking into account problems
MV(X) was facing previously. In theory the Model-View-ViewModel looks
very good. The View and the Model are already familiar to us, but also
the Mediator, represented as the View Model. MVVM It is pretty similar
to the MVP:
the MVVM treats the view controller as the View
There is no tight coupling between the View and the Model In addition, it does binding like the Supervising version of the MVP;
however, this time not between the View and the Model, but between the
View and the View Model. So what is the View Model in the iOS reality?
It is basically UIKit independent representation of your View and its
state. The View Model invokes changes in the Model and updates itself
with the updated Model, and since we have a binding between the View
and the View Model, the first is updated accordingly.
Also the MVVM Design Pattern and How does MVVM work?
Check it out this topic -> What is the difference between MVC and MVVM?
Model-View-ViewModel basic example is here -> https://github.com/futurice/mvvm-example-objc and maybe next step, you can use reactive cocoa because it can be more effective with mvvm -> https://github.com/ReactiveCocoa/ReactiveViewModel
reactive cocoa -> https://github.com/ReactiveCocoa/ReactiveCocoa
goodluck :)

Where to add persistent/networking code in a MVVM architecture

I'm trying to make a project for learning purpose using MVVM. I get the idea on how this pattern works, but all of the examples I found are very basic and don't explain where exactly you add your persistent/networking code.
I am trying to use Core Data in my project.
Does someone has experience with this pattern ? I am reading that this kind of code should go in a NSOperation. Does this make sense ?
Any other thoughts ? I am just looking for some advice from other people who done this.
For now I am not interested in a Reactive approach.
Since asynchronous networking code can stay live well outside (and past) anything to do with the current UI, it should probably be put in a model object or other separate controller object, completely outside the view, view controller or view model hierarchy. Perhaps in a network controller object (custom class) attached at the app delegate level, so that networking state can be retained and gracefully shut down or handled in the background.

Which iOS Architecture Pattern is better to optimize the code Can anyone explain?

I want to make the APP as I had already used MVC pattern for the development , but if anyone know which one is good architecture with proper explanation so I can go for that.
Appreciate with your help thanks!!
There are lots of different design pattern which you can use based on your requirement, scope of project. There are couple of articles which will guide you through these patterns, i'd just brief you abt the same.
List of some design pattern:
Singleton
List item
Delegate
Model View Controller
Observer
Facade
Command
Template Method
No one can say that one pattern is best and other is not, each patterns has its own features. Still IMO Model View Controller design patter is mostly used as lots of Cocoa iOS frameworks are based on this design pattern. So just see the difference between each and use the one which suites you requirement and don't think that you can only use one pattern at a time on in one project, you can use multiple design pattern in single project like MVC and SingleTone.
Ref:
https://www.raywenderlich.com/46988/ios-design-patterns
https://www.cs.colorado.edu/~kena/classes/5448/f12/presentation-materials/myrose.pdf
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52#.wl5ff762q
Hope that helps you.
Choosing appropriate architecture is completely based on the needs of your application.
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52#.58u55ykux

How does different components communicate in MVVM in iOS

knowing that in typical MVC implemented iOS projects the Model, view, Controller will communicate in the below patterns.
View to Controller using IBActions, Delegation
Controller to Model using direct method invocations or call backs using blocks
Model to Controller using Delegation or KVO
Controller to View using IBOutlets
correct me if i was wrong anyplace.
My question is how this would happen in a typical MVVM implemented Projects.I want the best communication approaches between components & please justify also how a particular one is better than others if anything has more than one way of communication availability.for example (Delegation & callBacks) mostly its preferable going with Call backs for more precise & readable code.
I will also up vote those who gives me the best answer.
Data Flow describe the communication of elements in MVVM:
Data Flow
1. UI calls method from ViewModel (Presenter).
2. ViewModel executes Use Case.
3. Use Case combines data from User and Repositories.
4. Each Repository returns data from a Remote Data (Network), Persistent DB Storage Source or In-memory Data (Remote or Cached).
5. Information flows back to the UI where we display the list of items.
In this article, there is a more detailed description of MVVM
https://tech.olx.com/clean-architecture-and-mvvm-on-ios-c9d167d9f5b3
First of all, I agree with you about what you pointed out about MVC. I just want to make a discussion about MVC and MVVM from my point of views
From the architecture point of view, in the typical MVC setup, Models represent data, Views represent user interfaces, and View Controllers mediate the interactions between the V and M. The relationships between VC and V, VC and M are tight coupling. Moreover, from that setup, VC will handle a lot of logics and become massive due to over responsibilities.
In MVVM, VC and V are be treated as a component, M still works as it is in MVC. The new component VM, which encapsulates data/logics that the V can bind to and any logics and action can be performed. The connection between V and VM is loose coupling. Why? because the V keeps referent of VM, however, the VM doesn't know the V and it just can be communication with the V via binding system.
From the advantages of MVVM in application development point of view, in which it comes with several benefits. We can easy to write Unit Test to test the logic of VM. We can easy to modify the V (UI) due to now it is just binding to VM.

Resources