IOS Architecture Patterns - IOS App Development - Medium
IOS Architecture Patterns - IOS App Development - Medium
Feeling weird while doing MVC in iOS? Have doubts about switching to
MVVM? Heard about VIPER, but not sure if it worth it?
Keep reading, and you will Dnd answers to questions above, if you don’t
— feel free to complain in comments.
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 1 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
these:
. . .
And this can happen, even despite the fact that you are following Apple’s
guidelines and implementing Apple’s MVC pattern, so don’t feel bad.
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 2 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
There is something wrong with the Apple’s MVC, but we’ll get back to it
later.
2. Testability usually comes from the Drst feature (and don’t worry: it
is easy with appropriate architecture).
Why Distribution?
Distribution keeps a fair load on our brain while we trying to Dgure out
how things work. If you think the more you develop the better your brain
will adapt to understanding complexity, then you are right. But this
ability doesn’t scale linearly and reaches the cap very quickly. So the
easiest way to defeat complexity is to divide responsibilities among
multiple entities following the single responsibility principle.
Why Testability?
This is usually not a question for those who already felt gratitude to unit
tests, which failed after adding new features or due to refactoring some
intricacies of the class. This means the tests saved those developers from
Dnding issues in runtime, which might happen when an app is on a
user’s device and the Dx takes a week to reach the user.
. . .
MV(X) essentials
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 3 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
MV(X) essentials
Nowadays we have many options when it comes to architecture design
patterns:
• MVC
• MVP
• MVVM
• VIPER
First three of them assume putting the entities of the app into one of 3
categories:
Let’s start with MV(X) patterns and get back to VIPER later.
MVC
How it used to be
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 4 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Apple’s MVC
Expectation
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 5 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Apple’s MVC
Reality
❤
1 var userCell = tableView.dequeueReusableCellWithIdentifier
2 userCell.configureWithUser(user)
The cell, which is the View conDgured directly with the Model, so MVC
guidelines are violated, but this happens all the time, and usually people
don’t feel it is wrong. If you strictly follow the MVC, then you supposed
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 6 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
to conDgure the cell from the controller, and don’t pass the Model into
the View, and this will increase the size of your Controller even more.
The problem might not be evident until it comes to the Unit Testing
(hopefully, it does in your project). Since your view controller is tightly
coupled with the view, it becomes didcult to test because you have to be
very creative in mocking views and their life cycle, while writing the
view controller’s code in such a way, that your business logic is separated
as much as possible from the view layout code.
1 import UIKit
2
3 struct Person { // Model
4 let firstName: String
5 let lastName: String
6 }
7
MVC example
In fact, loading and testing UIViews on one simulator (e.g. iPhone 4S)
doesn’t guarantee that it would work Dne on the other devices (e.g.
iPad), so I’d recommend to remove “Host Application” from your Unit
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 7 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Test target conDguration and run your tests without your application
running on simulator.
The interactions between the View and the Controller aren’t really testable
with Unit Tests
With all that said, it might seems that Cocoa MVC is a pretty bad pattern
to choose. But let’s assess it in terms of features deDned in the beginning
of the article:
Cocoa MVC is the pattern of your choice if you are not ready to invest
more time in your architecture, and you feel that something with higher
maintenance cost is an overkill for your tiny pet project.
Cocoa MVC is the best architectural pattern in terms of the speed of the
development.
MVP
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 8 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Doesn’t it look exactly like the Apple’s MVC? Yes, it does, and it’s name is
MVP (Passive View variant). But wait a minute… Does this mean that
Apple’s MVC is in fact a MVP? No, its not, because if you recall, there, the
View is tightly coupled with the Controller, while the MVP’s mediator,
Presenter, has nothing to do with the life cycle of the view controller,
and the View can be mocked easily, so there is no layout code in the
Presenter at all, but it is responsible for updating the View with data
and state.
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 9 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
1 import UIKit
2
3 struct Person { // Model
4 let firstName: String
5 let lastName: String
6 }
7
MVP example
MVP
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 10 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
MVVM
MVVM
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 11 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Bindings
I brieMy mentioned them in the MVP part, but let’s discuss them a bit
here. Bindings come out of box for the OS X development, but we don’t
have them in the iOS toolbox. Of course we have the KVO and
notiDcations, but they aren’t as convenient as bindings.
There is one bitter truth about reactive frameworks: the great power
comes with the great responsibility. It’s really easy to mess up things
when you go reactive. In other words, if you do something wrong, you
might spend a lot of time debugging the app, so just take a look at this
call stack.
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 12 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Reactive Debugging
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 13 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
1 import UIKit
2
3 struct Person { // Model
4 let firstName: String
5 let lastName: String
6 }
7
MVVM example
• Easy of use — its has the same amount of code as the MVP in our
example, but in the real app where you’d have to forward all events
from the View to the Presenter and to update the View manually,
MVVM would be much skinnier if you used bindings.
. . .
VIPER
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 14 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
VIPER
• Entities — your plain data objects, not the data access layer, because
that is a responsibility of the Interactor.
Basically, VIPER module can be a one screen or the whole user story of
your application — think of authentication, which can be one screen or
several related ones. How small are your “LEGO” blocks supposed to be?
— It’s up to you.
If we compare it with the MV(X) kind, we’ll see a few diQerences of the
distribution of responsibilities:
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 15 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
• Model (data interaction) logic shifted into the Interactor with the
Entities as dumb data structures.
Proper way of doing routing is a challenge for the iOS applications, the
MV(X) patterns simply don’t address this issue.
1 import UIKit
2
3 struct Person { // Entity (usually more complex e.g. NSManagedObject)
4 let firstName: String
5 let lastName: String
6 }
7
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 16 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
Conclusion
We went though several architectural patterns, and I hope you have
found some answers to what bothered you, but I have no doubt that you
realised that there is no silver bullet so choosing architecture pattern is
a matter of weighting tradeoQs in your particular situation.
. . .
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 17 de 18
iOS Architecture Patterns – iOS App Development – Medium 13/6/19 15(46
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 Página 18 de 18