Statistics
3
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-22

AuthorRaywenderlich Tutorial Team, Josh Berlin, René Cacheaux

Apply Different Architectures to Your Codebase!Advanced iOS App Architecture guides you through building one real-world app written in different architectures to give you hands-on and practical experience working in different architectures. This book will also guide you through the theory you need to gain a solid foundation of architecture concepts so that you can make your own informed decisions on how to use them in your codebase.Who This Book Is ForThis book is for intermediate iOS developers who already know the basics of iOS and are looking to build apps using defined architectures, making apps cleaner and easier to maintain.Topics Covered in Advanced iOS App ArchitectureNavigating Architecture Topics: Learn the theory behind various architectures to help inform which works best for you in different situations you may face. Managing Dependencies: Learn how to manage dependencies both internally and externally within your app. MVVM Architecture: Explore the history of the MVVM architecture and begin building KOOBER - the book's project app - using MVVM principles. Redux Architecture: Explore the history of the Redux architecture and continue building KOOBER using Redux principles. Elements Architecture: Explore the history of the Elements architecture and continue building KOOBER using Elements principles.After reading this book, you'll have the knowledge to decide which types of architecture components suit your apps and you'll have a deep understanding of the covered architectures.About the iOS Architecture TeamThe architecture team is a group of seasoned developers who work for large multi-national companies who deal with large and diverse code bases on a daily basis. The knowledge procured over years of development is now being transferred to you through book. We hope you enjoy the book and, hopefully, you'll apply some of the architectures you've learned to your own apps!

Tags
No tags
ISBN: 195032561X
Publisher: Razeware LLC
Publish Year: 2022
Language: 英文
File Format: PDF
File Size: 18.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Advanced iOS App Architecture By René Cacheaux & Josh Berlin Copyright ©2022 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. Advanced iOS App Architecture Advanced iOS App Architecture raywenderlich.com 2
Table of Contents: Overview Book License 7................................................................................................ Before You Begin 8................................................................... What You Need 9.......................................................................................... Book Source Code & Forums 11............................................................. Section I 14................................................................................. Chapter 1: Welcome 15.................................................................. Chapter 2: Which Architecture Is Right for Me? 18............ Chapter 3: Example App: Koober 38......................................... Chapter 4: Objects & Their Dependencies 53....................... Chapter 5: Architecture: MVVM 127....................................... Chapter 6: Architecture: Redux 185......................................... Chapter 7: Architecture: Elements, Part 1 239.................... Chapter 8: Architecture: Elements, Part 2 262.................... Conclusion 334.............................................................................................. Advanced iOS App Architecture raywenderlich.com 3
Table of Contents: Extended Book License 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Before You Begin 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Authors 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Editor 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I 14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1: Welcome 15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What lies ahead 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Who this book is for 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Which Architecture Is Right for Me? 18. . . . . . . . . . . . . Identifying problems to solve 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Boosting team velocity and strengthening code quality 19. . . . . . . . . . . . . . Examining the problems 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Increasing code agility 30. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Surveying architecture patterns 32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Selecting a pattern 34. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Putting patterns into practice 36. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 37. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Example App: Koober 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . Koober 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Koober? 46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started with the source 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 52. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Objects & Their Dependencies 53. . . . . . . . . . . . . . . . . . Advanced iOS App Architecture raywenderlich.com 4
Establishing the goals 54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Learning the lingo 55. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating dependencies 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The fundamental considerations 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why is this architecture? 60. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dependency patterns 60. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dependency Injection 61. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . On-demand approach 67. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Factories approach 69. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Single-container approach 75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Designing container hierarchies 78. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying DI theory to iOS apps 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying the on-demand approach 91. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying the factories approach 97. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying the single-container approach 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying the container hierarchy approach 118. . . . . . . . . . . . . . . . . . . . . . . . . Key points 125. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Architecture: MVVM 127. . . . . . . . . . . . . . . . . . . . . . . . . . . What is it? 128. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Container views 134. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Communicating amongst view models 136. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Navigating 137. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying theory to iOS apps 141. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Composing views 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Navigating 163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Managing state 176. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pros and cons of MVVM 182. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 183. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Architecture: Redux 185. . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced iOS App Architecture raywenderlich.com 5
What is Redux? 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying theory to iOS apps 198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pros and cons of Redux 236. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 238. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 238. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Architecture: Elements, Part 1 239. . . . . . . . . . . . . . . . . Introducing Elements 241. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Underlying concepts of Elements 242. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . User interface 246. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interaction responder 254. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 261. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: Architecture: Elements, Part 2 262. . . . . . . . . . . . . . . . . Observer 262. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Use case 299. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pros and cons of Elements 332. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 333. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 334. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced iOS App Architecture raywenderlich.com 6
LBook License By purchasing Advanced iOS App Architecture, you have the following license: • You are allowed to use and/or modify the source code in Advanced iOS App Architecture in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Advanced iOS App Architecture in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Advanced iOS App Architecture, available at www.raywenderlich.com”. • The source code included in Advanced iOS App Architecture is for your personal use only. You are NOT allowed to distribute or sell the source code in Advanced iOS App Architecture without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or 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 an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 7
Before You Begin This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more. raywenderlich.com 8
iWhat You Need To follow along with this book, you’ll need the following: • A Mac running macOS Big Sur or later. • Swift 5.5.2: all projects have been written to work with Swift 5.5.2 in Xcode. • Xcode 13.2.1 or later. You’ll need Xcode 13.2.1 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.5.2 and Xcode 13.2.1. 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. raywenderlich.com 9
The prerequisites for this book include an intermediate understanding of Swift and iOS development. If you’ve worked through our classic beginner books — Swift Apprentice (https://www.raywenderlich.com/books/swift-apprentice) and UIKit Apprentice (https://www.raywenderlich.com/books/uikit-apprentice) — or have similar development experience, you’re ready to read this book. To get the most out of this book an understanding of Apple’s new Combine framework would also be helpful. If you’ve worked through our - Combine Asynchronous Programming With Swift (https://www.raywenderlich.com/books/ combine-asynchronous-programming-with-swift) book you’ll understand more regarding the reactive programming used within 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. Advanced iOS App Architecture What You Need raywenderlich.com 10
iiBook Source Code & Forums Where to download the materials for this book The materials for this book can be cloned or downloaded from the GitHub book materials repository: • https://github.com/raywenderlich/arch-materials/tree/editions/4.0 Forums We’ve also set up an official forum for the book at https:// forums.raywenderlich.com/c/books/advanced-ios-architecture. This is a great place to ask questions about the book or to submit any errors you may find. raywenderlich.com 11
“To my beautiful wife Lauren, to my fun-loving angel Zara, to my 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 12
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 atHatch building products to help people sleep better. He’s built apps for the iPhone and iPad since 2008. Josh recently finished culinaryschool 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 Editor Darren Ferguson is doubling as the tech editor and 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 (https:// twitter.com/darren102). Advanced iOS App Architecture About the Team raywenderlich.com 13
Section I raywenderlich.com 14
1Chapter 1: Welcome Welcome to Advanced iOS App Architecture. The main goal of this book is to thoroughly explain and show how to apply popular iOS app architectures, one by one. We can’t wait for you to explore the architectures covered in the following chapters. We absolutely love this topic. We are super passionate about architecture because architecture unlocks the ability for teams to grow and go quickly. Now, more than ever, it’s very important to understand and apply good software architecture practices in our projects as apps are getting more complex and as development teams are pressured to deliver faster results despite constantly changing requirements. raywenderlich.com 15
What lies ahead Chapter 1 through Chapter 4 introduce you to different aspects of the material covered in the book. We recommend reading these chapters before diving into any of the architecture chapters. Chapter 5 through Chapter 8 are architecture chapters; in other words, they explore one architecture at a time. Each architecture chapter begins with a little history followed by a detailed theory walkthrough. The second half of each architecture chapter focuses on applying the theory to iOS app development. Each architecture chapter ends by covering the pros and cons of that architecture. Feel free to read these latter chapters in any order. There are many architectures not covered in this book because we wanted to go deep instead of broad. Who this book is for This book is for iOS developers who build apps using Swift. The material in this book assumes familiarity with design patterns and with basic architectures — such as MVC — and basic architecture concepts, such as inversion of control. This book also assumes familiarity with Apple frameworks such as Combine. If you’re new to Swift, check out the raywenderlich.com Swift Apprentice (https:// www.raywenderlich.com/books/swift-apprentice) book; for a refresher on design patterns, check out the raywenderlich.com Design Patterns by Tutorials (https:// www.raywenderlich.com/books/design-patterns-by-tutorials); for an introduction to Combine, check out the raywenderlich.com Combine: Asynchronous Programming With Swift (https://www.raywenderlich.com/books/combine-asynchronous- programming-with-swift) Advanced iOS App Architecture Chapter 1: Welcome raywenderlich.com 16
Where to go from here? The next three chapters are designed to be introductions, so give them a read. Then, find the chapters for the architectures you are most interested in learning and go for a deep dive into each. If you aren’t sure which architectures you’d like to explore, we recommend reading the theory section of all the architecture chapters first in order to identify which architectures fit your needs the most. Then you can take a deep dive by reading the iOS app portion of the chapters you found most compelling. Our hope is that, after reading this book, you will be able to apply different app architectures to different projects in a way that will unleash your team’s ability to build quickly and soundly. Happy architecting! Advanced iOS App Architecture Chapter 1: Welcome raywenderlich.com 17
2Chapter 2: Which Architecture Is Right for Me? By René Cacheaux You might be wondering: Which architecture pattern is right for me? Honestly, there’s no perfect universal app architecture. An architecture that works best for one project might not work best for your project. There are many different aspects to consider when establishing an architecture for you and your team to follow. This chapter guides you through the process of finding the best architecture for your project. There’s a lot that goes into shaping your app’s codebase into a cohesive and effective architecture. Knowing where to start can especially be overwhelming. Every single file in your app’s codebase plays a part in your app’s architecture. There’s no shortage of architecture patterns. Unfortunately, most patterns only scratch the surface and leave you to figure out the fine details. In addition, many patterns are similar to one another and have only minor differences here and there. All of this makes architecture hard to put into practice. Fortunately, there are pragmatic steps you can take to ensure your architecture is effective: 1. Understand the current state of your codebase. 2. Identify problems you’d like to solve or code you’d like to improve. 3. Evaluate different architecture patterns. 4. Try a couple patterns on for size before committing to one. raywenderlich.com 18
5. Draw a line in the sand and define your app’s baseline architecture. 6. Look back and determine if your architecture is effectively addressing the problems you want to solve. 7. Iterate and evolve your app’s architecture over time. Notice how selecting an architecture pattern isn’t the first item on the list. The reality is that selecting an architecture pattern is less important than understanding the the problems you’re trying to solve using architectural patterns. Taking the time to understand the problems you want to solve allows you to focus on the few aspects of architecture that really make a difference. While many problems will be specific to your project, there are several general problems you can solve through good architecture. The next several sections cover these general problems in detail. Identifying problems to solve Before embarking on any architecture project, you should first identify and understand the problems you’d like to solve. This will allow you to evaluate whether you’re getting the most out of your app’s architecture. A good architecture enables you and your team to easily and safely change code without a ton of risk. Making changes to code in a codebase that’s not architected well is expensive and risky. The two primary problems that good architecture practices solve are slow team velocity and fragile code quality. Additionally, good architecture practices can help you prevent rigid software. The next sections cover these two primary problems followed by a section that covers rigid software. Boosting team velocity and strengthening code quality A good app architecture enables you to deliver features and bug fixes faster without compromising on quality. On the other hand, a less-than-ideal architecture slows your team down and makes your codebase very difficult to change without breaking existing functionality. Knowing this, what problems should you be looking for? Which problems can architecture solve? Advanced iOS App Architecture Chapter 2: Which Architecture Is Right for Me? raywenderlich.com 19
Here are several problems that, when present, lead to slow velocity and fragile code quality: • My app’s codebase is hard to understand. • Changing my app’s codebase sometimes causes regressions. • My app exhibits fragile behavior when running. • My code is hard to re-use. • Changes require large code refactors. • My teammates step on each other’s toes. • My app’s codebase is hard to unit test. • My team has a hard time breaking user stories into tasks. • My app takes a long time to compile. You can solve these problems by applying architecture concepts. All of these problems have common root causes. Walking through some of the root causes will help set the stage for studying each of these problems in detail. Understanding root causes Each of these problems can be caused by two fundamental root causes: highly interdependent code and large types. Understanding these root causes is important when creating a plan for boosting team velocity and strengthening code quality. So what exactly are these root causes, and how do you know if they’ve made it into your codebase? That’s next. Advanced iOS App Architecture Chapter 2: Which Architecture Is Right for Me? raywenderlich.com 20