Advanced iOS App Architecture
Advanced iOS App Architecture
By René Cacheaux & Josh Berlin
Copyright ©2019 Razeware LLC.
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an “as
is” basis, without warranty of any kind, express of implied, including but not limited to
the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in the
software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com
2
Advanced iOS App Architecture
Dedications
"To my beautiful wife Lauren, to my fun-loving angel Zara, to my
soon-to-arrive son René Jr., to my parents who have given me
everything, and, last but not least, to my furry pals Paco and Charlie.
I love you all."
— René Cacheaux
"Thanks to my parents for buying me my first, second, and third
computers, and making me put them together myself. Thanks for
allowing me to take any path I wanted in life, even when it’s a little
crazy. Love y'all."
— Josh Berlin
raywenderlich.com
3
Advanced iOS App Architecture
About the Authors
Josh Berlin is an author of this book. He loves building thoughtful
user experiences on mobile. He’s currently an iOS engineer at Cruise
Automation making apps for self-driving cars. He's built apps for the
iPhone and iPad since 2008. Josh recently finished culinary school in
Austin, TX. When he's not coding, he's probably cooking or dreaming
of food.
René Cacheaux is an author of this book. He loves to architect and
build software. He currently is a Mobile Architect at Atlassian where
his mission is to design Atlassian's mobile platform. He especially
loves all things mobile and currently architects for both Android and
Apple platforms. René has been engineering iOS apps since 2009 and
has experience in mobile client and server engineering, mobile user
experience design and product management. René has worked on a
wide range of apps spanning from industrial sales enablement to
world-wide social networking. René enjoys starting his days in true
Austin-Texas fashion with a a breakfast taco alongside a freshly
brewed cappuccino. In addition to building mobile apps, he loves to
travel, snow ski, ocean kayak and root for his alma mater, the Texas
Longhorns.
About the Editors
Aaron Douglas is a tech editor for this book. He was that kid taking
apart the mechanical and electrical appliances at five years of age to
see how they worked. He never grew out of that core interest - to
know how things work. He took an early interest in computer
programming, figuring out how to get past security to be able to play
games on his dad's computer. He's still that feisty nerd, but at least
now he gets paid to do it. Aaron works for Automattic
(WordPress.com, WooCommerce, SimpleNote) as a Mobile Lead
primarily on the WooCommerce mobile apps. Find Aaron on Twitter
as @astralbodies or at his blog at https://aaron.blog.
raywenderlich.com
4
Advanced iOS App Architecture
Joshua Greene is a tech editor for this book. He is an experienced iOS
developer who loves creating elegant apps. When he's not slinging
code, he enjoys martial arts, Netflix and spending time with his
wonderful wife and two daughters. You can reach him on Twitter at
@jrg_developer.
Manda Frederick is an editor of this book. She has been involved in
publishing for over ten years through various creative, educational,
medical and technical print and digital publications, and is thrilled to
bring her experience to the raywenderlich.com family as Managing
Editor. In her free time, you can find her at the climbing gym,
backpacking in the backcountry, hanging with her dog, working on
poems, playing guitar and exploring breweries.
Darren Ferguson is the final pass editor for this book. He's an
experienced software developer and works for M.C. Dean, Inc, a
systems integration provider from North Virginia. When he's not
coding, you'll find him enjoying EPL Football, traveling as much as
possible and spending time with his wife and daughter. Find Darren
on Twitter at @darren102.
About the Artist
Vicki Wenderlich is the designer and artist of the cover of this book.
She is Ray’s wife and business partner. She is a digital artist who
creates illustrations, game art and a lot of other art or design work for
the tutorials and books on raywenderlich.com. When she’s not
making art, she loves hiking, a good glass of wine and attempting to
create the perfect cheese plate.
raywenderlich.com
5
Advanced iOS App Architecture
Table of Contents: Overview
What You Need
Book License
Book Source Code & Forums
About the Cover
Chapter 1: Welcome
Chapter 2: Which Architecture Is Right for Me?
Chapter 3: Example App: Koober
Chapter 4: Objects & Their Dependencies
Chapter 5: Architecture: MVVM
Chapter 6: Architecture: Redux
Chapter 7: Architecture: Elements, Part 1
Chapter 8: Architecture: Elements, Part 2
Conclusion
........................................................
............................................................
...................................
.......................................................
.................................................
.......
............................
..............
.............................
..............................
...............
.............
............................................................
10
11
12
13
14
16
34
47
115
167
212
233
297
raywenderlich.com
6
Advanced iOS App Architecture
Table of Contents: Extended
What You Need
Book License
Book Source Code & Forums
About the Cover
Chapter 1: Welcome
What lies ahead
Who this book is for
Where to go from here?
Chapter 2: Which Architecture Is Right for Me?
Identifying problems to solve
Boosting team velocity and strengthening code quality
Examining the problems
Increasing code agility
Surveying architecture patterns
Selecting a pattern
Putting patterns into practice
Key points
Chapter 3: Example App: Koober
Koober
Why Koober?
Getting started with the source
Key points
Chapter 4: Objects & Their Dependencies
Establishing the goals
Learning the lingo
Creating dependencies
The fundamental considerations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
11
12
13
14
14
15
15
16
17
17
19
26
29
31
32
33
34
34
41
41
46
47
47
48
51
52
raywenderlich.com
7
Advanced iOS App Architecture
Why is this architecture?
Dependency patterns
Dependency Injection
On-demand approach
Factories approach
Single-container approach
Designing container hierarchies
Applying DI theory to iOS apps
Applying the on-demand approach
Applying the factories approach
Applying the single-container approach
Applying the container hierarchy approach
Key points
Where to go from here?
Chapter 5: Architecture: MVVM
What is it?
Container views
Communicating amongst view models
Navigating
Applying theory to iOS apps
Composing views
Navigating
Managing state
Key points
Pros and cons of MVVM
Where to go from here?
Chapter 6: Architecture: Redux
History
What is Redux?
Applying theory to iOS apps
Key points
Pros and cons of Redux
Where to go from here?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
54
55
59
61
67
69
74
82
88
100
107
113
114
115
116
122
124
125
128
139
148
159
164
164
166
167
167
168
179
209
209
211
raywenderlich.com
8