ebook img

Advanced iOS App Architecture (1st Edition) - 2019 PDF

297 Pages·02.162 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced iOS App Architecture (1st Edition) - 2019

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........................................................ 10 Book License ............................................................ 11 Book Source Code & Forums ................................... 12 About the Cover....................................................... 13 Chapter 1: Welcome................................................. 14 Chapter 2: Which Architecture Is Right for Me? ....... 16 Chapter 3: Example App: Koober ............................ 34 Chapter 4: Objects & Their Dependencies.............. 47 Chapter 5: Architecture: MVVM ............................. 115 Chapter 6: Architecture: Redux.............................. 167 Chapter 7: Architecture: Elements, Part 1............... 212 Chapter 8: Architecture: Elements, Part 2 ............. 233 Conclusion ............................................................ 297 raywenderlich.com 6 Advanced iOS App Architecture Table of Contents: Extended What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Book License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Book Source Code & Forums. . . . . . . . . . . . . . . . . . . . . . 12 About the Cover. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Chapter 1: Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 What lies ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Who this book is for. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 2: Which Architecture Is Right for Me? . . . . . 16 Identifying problems to solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Boosting team velocity and strengthening code quality . . . . . . . . . . . 17 Examining the problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Increasing code agility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Surveying architecture patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Selecting a pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Putting patterns into practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 3: Example App: Koober . . . . . . . . . . . . . . . . . 34 Koober . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Why Koober? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Getting started with the source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Chapter 4: Objects & Their Dependencies. . . . . . . . . 47 Establishing the goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Learning the lingo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Creating dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 The fundamental considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 raywenderlich.com 7 Advanced iOS App Architecture Why is this architecture?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Dependency patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 On-demand approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Factories approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Single-container approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Designing container hierarchies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Applying DI theory to iOS apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Applying the on-demand approach . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Applying the factories approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Applying the single-container approach . . . . . . . . . . . . . . . . . . . . . . 100 Applying the container hierarchy approach . . . . . . . . . . . . . . . . . . . . 107 Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Chapter 5: Architecture: MVVM . . . . . . . . . . . . . . . . . . 115 What is it?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Container views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Communicating amongst view models. . . . . . . . . . . . . . . . . . . . . . . . 124 Navigating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Applying theory to iOS apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Composing views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Navigating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Managing state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Pros and cons of MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Chapter 6: Architecture: Redux . . . . . . . . . . . . . . . . . . 167 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 What is Redux? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Applying theory to iOS apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Pros and cons of Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 raywenderlich.com 8 Advanced iOS App Architecture Chapter 7: Architecture: Elements, Part 1 . . . . . . . . . 212 Introducing Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Underlying concepts of Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 User interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Interaction responder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Chapter 8: Architecture: Elements, Part 2 . . . . . . . . 233 Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Use case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Pros and cons of Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 raywenderlich.com 9 W What You Need To follow along with this book, you'll need the following: • A Mac running macOS Mojave (10.14.3) or later. • Swift 5: all projects have been written to work with Swift 5 in Xcode. • Xcode 10.2 or later. You'll need Xcode 10.2 or later to open and run the example apps included in this book. If you haven't installed the latest version of macOS or Xcode, be sure to do that before continuing with the book. The code covered in this book depends on Swift 5 and Xcode 10.2. This book provides the building blocks for developers who wish to broaden their horizons and learn how architectures can help them build robust and maintainable applications and SDKs. The only prerequisites for this book are an intermediate understanding of Swift and iOS development. If you’ve worked through our classic beginner books — Swift Apprentice https://store.raywenderlich.com/products/swift-apprentice and iOS Apprentice https:// store.raywenderlich.com/products/ios-apprentice — or have similar development experience, you’re ready to read this book. As you work through the book, you’ll be taken through a deep dive into different architectures for a fictional app named Koober. Each chapter will explain the theory behind each of the architectures first. The second half of the chapters will guide you through how the Koober application utilized the architecture and show you how the architecture was used within the application. raywenderlich.com 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.