Statistics
7
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-15

AuthorScott Gardner, Shai Mishali, Florent Pillet, Marin Todorov, the raywenderlich Tutorial Team

Dive into Combine! Writing asynchronous code can be challenging, with a variety of possible interfaces to represent, perform, and consume asynchronous work — delegates, notification center, KVO, closures, etc. Juggling all of these different mechanisms can be somewhat overwhelming. Does it have to be this hard? Not anymore! In this book, you’ll learn about Combine — Apple’s framework to work with asynchronous events in a unified and reactive way that ensures your app is always up to date based on the latest state of its data. Who This Book Is For This book is for intermediate iOS developers who already know the basics of iOS and Swift development but are interested in learning declarative/reactive programming and take their app and state management to the next level. You’ll also find this book interesting if you’re interested in SwiftUI — as many of the reactive capabilities keeping your SwiftUI views up-to-date are built on top of Combine. Topics Covered in Combine: Asynchronous Programming with Swift • What & Why: Learn what is Combine and reactive programming and the problems they solve, and how you can unify all of your asynchronous piece of work. • Operators: Learn how to compose, transform, filter and otherwise manipulate different pieces of asynchronous work using operators. • In Practice: You’ll gain knowledge on various topics and techniques you’ll leverage when writing your own real-life apps, as well as practice these techniques with actual hands-on apps and projects. • SwiftUI: You’ll learn about how Combine is deeply rooted within SwiftUI and provides it with the ability to reactively update its views based on the state of your app. • Advanced Combine: Once you’ve got a handle on the basics, you’ll dive into advanced Combine topics such as Error Handling, • Schedulers, and Custom Publishers. By the end of this book, you’ll be a pro in building full-fledged applications using Combine’s various abilities.

Tags
No tags
ISBN: 1950325334
Publisher: Ray Wenderlich; Razeware LLC
Publish Year: 2020
Language: 英文
Pages: 442
File Format: PDF
File Size: 21.2 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)
Combine: Asynchronous Programming with Swift By Scott Gardner, Shai Mishali, Florent Pillet & Marin Todorov Copyright ©2020 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. Combine: Asynchronous Programming with Swift raywenderlich.com 2
Dedications “For Jenn and Charlotte” — Scott Gardner “For my wife Elia and Baby Ethan—my love, inspiration, and rock ❤ . To my family and friends for their support: Dad, Mom, Ziv, Adam, and everyone else, you’re the best!” — Shai Mishali “To Fabienne and Alexandra ❤ .” — Florent Pillet “To my father. To my mom. To Mirjam and our beautiful daughter.” — Marin Todorov Combine: Asynchronous Programming with Swift raywenderlich.com 3
About the Authors Scott Gardner is an author and the technical editor for this book. Scott is a veteran iOS app architect and developer with over ten years of experience. He’s authored several books, video courses, tutorials, and articles on Swift and iOS app development, with a focus on reactive programming. Additionally, Scott is an Apple Certified Trainer for Swift and iOS, and he also enjoys speaking at conferences, meetups, and online events. Say hello to Scott on Twitter at @scotteg, or connect with him on LinkedIn at scotteg.com. Shai Mishali is an author and the final pass editor on this book. He’s the iOS Tech Lead for Gett, the global on-demand mobility company; as well as an international speaker, and a highly active open-source contributor and maintainer on several high-profile projects - namely, the RxSwift Community and RxSwift projects, but also releases many open-source endeavors around Combine such as CombineCocoa, RxCombine and more. As an avid enthusiast of hackathons, Shai took 1st place at BattleHack Tel- Aviv 2014, BattleHack World Finals San Jose 2014, and Ford’s Developer Challenge Tel-Aviv 2015. You can find him on GitHub and Twitter as @freak4pc. Florent Pillet is an author of this book. He has been developing for mobile platforms since the last century and moved to iOS on day 1. He adopted reactive programming before Swift was announced, using it in production since 2015. A freelance developer, Florent also uses reactive programming on the server side as well as on Android and likes working on tools for developers like the popular NSLogger when he’s not contracting, training or reviewing code for clients worldwide. Say hello to Florent on Twitter and GitHub at @fpillet. Combine: Asynchronous Programming with Swift raywenderlich.com 4
Marin Todorov is an author of this book. Marin is one of the founding members of the raywenderlich.com team and has worked on eight of the team’s books. He’s an independent contractor and has worked for clients like Roche, Realm, and others. Besides crafting code, Marin also enjoys blogging, teaching and speaking at conferences. He happily open-sources code. You can find out more about Marin at www.underplot.com. 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. Combine: Asynchronous Programming with Swift raywenderlich.com 5
Table of Contents: Overview Book License 15............................................................................................. Before You Begin 16................................................................ What You Need 17........................................................................................ Book Source Code & Forums 18............................................................. About the Cover 19...................................................................................... Section I: Introduction to Combine 20.............................. Chapter 1: Hello, Combine! 21.................................................... Chapter 2: Publishers & Subscribers 37.................................. Section II: Operators 69......................................................... Chapter 3: Transforming Operators 70................................... Chapter 4: Filtering Operators 88............................................. Chapter 5: Combining Operators 109...................................... Chapter 6: Time Manipulation Operators 134..................... Chapter 7: Sequence Operators 161........................................ Chapter 8: In Practice: Project "Collage" 182........................ Section III: Combine in Action 206..................................... Chapter 9: Networking 207.......................................................... Chapter 10: Debugging 213......................................................... Chapter 11: Timers 219................................................................. Chapter 12: Key-Value Observing 225.................................... Chapter 13: Resource Management 232................................ Combine: Asynchronous Programming with Swift raywenderlich.com 6
Chapter 14: In Practice: Project "News" 240........................ Section IV: Advanced Combine 258................................... Chapter 15: In Practice: Combine & SwiftUI 259................ Chapter 16: Error Handling 288................................................. Chapter 17: Schedulers 315......................................................... Chapter 18: Custom Publishers & Handling Backpressure 339............................................................................. Chapter 19: Testing 375................................................................. Section V: Building a Complete App 397.......................... Chapter 20: In Practice: Building a Complete App 398..... Conclusion 424.............................................................................................. Combine: Asynchronous Programming with Swift raywenderlich.com 7
Table of Contents: Extended Book License 15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Before You Begin 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Cover 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I: Introduction to Combine 20. . . . . . . . . . . . . . . . Chapter 1: Hello, Combine! 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Asynchronous programming 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Foundation of Combine 26. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine basics 26. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s the benefit of Combine code over “standard” code? 32. . . . . . . . . . App architecture 33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book projects 34. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 36. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 36. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Publishers & Subscribers 37. . . . . . . . . . . . . . . . . . . . . . . . . Getting started 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Publisher 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Subscriber 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Cancellable 45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding what’s going on 46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a custom subscriber 49. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Future 52. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Subject 54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dynamically adjusting demand 61. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Type erasure 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 8
Challenge 64. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 67. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 68. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section II: Operators 69. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Transforming Operators 70. . . . . . . . . . . . . . . . . . . . . . . . . Getting started 71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collecting values 71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mapping values 74. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flattening publishers 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing upstream output 80. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Incrementally transforming output 82. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 85. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Filtering Operators 88. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 89. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Filtering basics 89. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compacting and ignoring 92. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding values 94. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dropping values 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Limiting values 102. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 107. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 108. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 108. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Combining Operators 109. . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Prepending 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appending 116. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced combining 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 9
Where to go from here? 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Time Manipulation Operators 134. . . . . . . . . . . . . . . . . . Getting started 135. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shifting time 137. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collecting values 140. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collecting values (part 2) 143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Holding off on events 144. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Timing out 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Measuring time 154. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Sequence Operators 161. . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding values 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Querying the publisher 171. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 181. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 181. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: In Practice: Project "Collage" 182. . . . . . . . . . . . . . . . . . . Getting started with “Collage” 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Talking to other view controllers 188. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wrapping a callback function as a future 192. . . . . . . . . . . . . . . . . . . . . . . . . . . . Presenting a view controller as a future 196. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharing subscriptions 197. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishing properties with @Published 198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operators in practice 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 203. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section III: Combine in Action 206. . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 10
Chapter 9: Networking 207. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . URLSession extensions 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Codable support 209. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishing network data to multiple subscribers 210. . . . . . . . . . . . . . . . . . . Key points 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 10: Debugging 213. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Printing events 214. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Acting on events — performing side effects 215. . . . . . . . . . . . . . . . . . . . . . . . . Using the debugger as a last resort 217. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 11: Timers 219. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using RunLoop 220. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the Timer class 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using DispatchQueue 222. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 224. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 224. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: Key-Value Observing 225. . . . . . . . . . . . . . . . . . . . . . . . . . Introducing publisher(for:options:) 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preparing and subscribing to your own KVO-compliant properties 226 ObservableObject 230. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 231. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 231. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Resource Management 232. . . . . . . . . . . . . . . . . . . . . . . The share() operator 233. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The multicast(_:) operator 235. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Future 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 11
Chapter 14: In Practice: Project "News" 240. . . . . . . . . . . . . . . . . . . Getting started with the Hacker News API 241. . . . . . . . . . . . . . . . . . . . . . . . . Getting a single story 242. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multiple stories via merging publishers 247. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting the latest stories 250. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 256. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section IV: Advanced Combine 258. . . . . . . . . . . . . . . . . . . Chapter 15: In Practice: Combine & SwiftUI 259. . . . . . . . . . . . . . . Hello, SwiftUI! 262. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started with “News” 265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A first taste of managing view state 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fetching the latest stories 268. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using ObservableObject for model types 270. . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying errors 273. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Subscribing to an external publisher 274. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Initializing the app’s settings 277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Editing the keywords list 279. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 286. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 16: Error Handling 288. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 289. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Never 289. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dealing with failure 296. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 314. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 314. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 17: Schedulers 315. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . An introduction to schedulers 316. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 12
Operators for scheduling 317. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scheduler implementations 322. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 336. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 338. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 338. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: Custom Publishers & Handling Backpressure 339 Creating your own publishers 340. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishers as extension methods 340. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The subscription mechanism 342. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishers emitting values 344. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishers transforming values 352. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling backpressure 367. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 19: Testing 375. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 376. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing Combine operators 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing production code 386. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 393. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section V: Building a Complete App 397. . . . . . . . . . . . . . Chapter 20: In Practice: Building a Complete App 398. . . . . . . . . Getting started 399. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting goals 401. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing JokesViewModel 402. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wiring JokesViewModel up to the UI 407. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing Core Data with Combine 413. . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 421. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 422. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 13
Where to go from here? 422. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 424. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combine: Asynchronous Programming with Swift raywenderlich.com 14
LBook License By purchasing Combine: Asynchronous Programming with Swift, you have the following license: • You are allowed to use and/or modify the source code in Combine: Asynchronous Programming with Swift 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 Combine: Asynchronous Programming with Swift in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/ designs: from Combine: Asynchronous Programming with Swift, available at www.raywenderlich.com”. • The source code included in Combine: Asynchronous Programming with Swift is for your personal use only. You are NOT allowed to distribute or sell the source code in Combine: Asynchronous Programming with Swift 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 or 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 15
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 16
iWhat You Need To follow along with this book, you’ll need the following: • A Mac running macOS Catalina (10.15) or later. Earlier versions might work, but they’re untested. • Xcode 12 or later. Xcode is the main development tool for iOS. You’ll need Xcode 12 or later for the tasks in this book. Since Combine was introduced with the iOS 13 SDK, Xcode 11 might work as well - but the code in this book was tested against Xcode 12. You can download the latest version of Xcode from Apple’s developer site here: apple.co/2asi58y • An intermediate level knowledge of Swift. This book teaches you how to write declarative and reactive iOS applications using Apple’s Combine framework. Combine uses a multitude of advanced Swift features such as generics, so you should have at least an intermediate-level knowledge of Swift. If you want to try things out on a physical iOS device, you’ll need a developer account with Apple, which you can obtain for free. However, all the sample projects in this book will work just fine in the iOS Simulator bundled with Xcode, so a paid developer account is completely optional. raywenderlich.com 17
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/comb-materials/tree/editions/2.0 Forums We’ve also set up an official forum for the book at https://forums.raywenderlich.com/ c/books/combine-asynchronous-programming-with-swift/57. This is a great place to ask questions about the book or to submit any errors you may find. raywenderlich.com 18
iiiAbout the Cover Combine Cover The polar bear, although majestic on land, is also a passionate creature of the sea; its scientific name, Ursus maritimus, means “sea bear”! Most polar bears spend the majority of their lives on the edge of sea ice, waiting for a seal to swim by and make a tasty lunch for the polar bear; for this reason, they are considered a marine mammal, in the same vein as sea otters, manatees, and whales. The sight of a polar bear cub instantly evokes that cute squeee feeling in all of us, but make no mistake — these cuddly little cubs will soon grow into stealthy, patient, and powerful adult hunters. The Combine framework, as well, is still in its infancy, and even though there are other powerful competing frameworks like RxSwift, we think that as Combine grows up, it will soon turn out to be as powerful, efficient and stealthy as a polar bear — just not quite as cute! raywenderlich.com 19
Section I: Introduction to Combine In this part of the book, you’re going to ramp up over the basics of Combine and learn about some of the building blocks it comprises. You’ll learn what Combine aims to solve and what are some of the abstractions it provides to help you solve them: Publisher, Subscriber, Subscription, Subject and much more. raywenderlich.com 20