Table Of ContentRxSwift - Reactive Programming with Swift
RxSwift: Reactive Programming with Swift
Florent Pillet, Junior Bontognali, Marin Todorov & Scott Gardner
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
RxSwift - Reactive Programming with Swift
Dedications
"To my father. To my mom. To Mirjam and our beautiful daughter."
— Marin Todorov
"For Fabienne and Alexandra"
— Florent Pillet
"For my grandfather."
— Junior Bontognali
"For Charlotte"
— Scott Gardner
raywenderlich.com 3
RxSwift - Reactive Programming with Swift
About the Authors
Florent Pillet is an author of this book. Florent 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 and
has been using RxSwift in production since 2015. A freelance
developer, Florent also uses Rx on Android and likes working on tools
for developers like the popular NSLogger when he's not contracting
for clients worldwide. Say hello to Florent on Twitter at @fpillet.
Junior Bontognali is an author of this book. Junior has been
developing on iOS since the first iPhone and joined the RxSwift team
in the early development stage. Based in Switzerland, when he's not
eating cheese or chocolate, he's doing some cool stuff in the mobile
space, without denying to work on other technologies. Other than
that he organizes tech events, speaks and blogs. Say hello to Junior on
Twitter at @bontoJR.
Marin Todorov is an author of this book. Marin is one of the
founding members of the raywenderlich.com team and has worked on
seven of the team's books. 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.
Scott Gardner is an author of this book. Scott has been developing
iOS apps since 2010, Swift since the day it was announced, and
RxSwift since before version 1. He's authored several video courses,
tutorials, and articles on iOS app development, presented at
numerous conferences, meetups, and online events, and this is his
second book. Say hello to Scott on Twitter at @scotteg.
raywenderlich.com 4
RxSwift - Reactive Programming with Swift
About the Editors
Ash Furrow is the technical editor of this book. Ash is a Canadian
iOS developer and author, currently working at Artsy. He has
published a number of books, built many apps, and is a contributor to
the open source community. On his blog ashfurrow.com, he writes
about a range of topics, from interesting programming to
explorations of analogue film photography.
Chris Belanger is the editor of this book. Chris Belanger is the Book
Team Lead and Lead Editor for raywenderlich.com. If there are words
to wrangle or a paragraph to ponder, he‘s on the case. When he kicks
back, you can usually find Chris with guitar in hand, looking for the
nearest beach, or exploring the lakes and rivers in his part of the
world in a canoe.
Shai Mishali is the final pass editor of this book. He's iOS Tech Lead
for Gett, the on-demand mobility company, and is involved in several
open source projects on his spare time - mainly the
RxSwiftCommunity and RxSwift projects, as well as an international
speaker.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.
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
RxSwift - Reactive Programming with Swift
Table of Contents: Overview
Introduction ............................................................................. 16
Section I: Getting Started with RxSwift ........................ 22
Chapter 1: Hello, RxSwift!.......................................... 23
Chapter 2: Observables .............................................. 45
Chapter 3: Subjects....................................................... 69
Chapter 4: Observables & Subjects in Practice. 85
Section II: Operators & Best Practices........................ 106
Chapter 5: Filtering Operators.............................. 108
Chapter 6: Filtering Operators in Practice....... 125
Chapter 7: Transforming Operators.................... 147
Chapter 8: Transforming Operators in
Practice ........................................................................... 164
Chapter 9: Combining Operators......................... 185
Chapter 10: Combining Operators in
Practice ........................................................................... 207
Chapter 11: Time-Based Operators.................... 227
Section III: iOS Apps with RxCocoa.............................. 246
Chapter 12: Beginning RxCocoa........................... 247
Chapter 13: Intermediate RxCocoa .................... 268
Section IV: Intermediate RxSwift/RxCocoa.............. 290
raywenderlich.com 6
RxSwift - Reactive Programming with Swift
Chapter 14: Error Handling in Practice.............. 291
Chapter 15: Intro to Schedulers............................ 311
Chapter 16: Testing with RxTest........................... 326
Chapter 17: Creating Custom Reactive
Extensions...................................................................... 342
Section V: RxSwift Community Cookbook................ 358
Chapter 18: Table & Collection Views................ 359
Chapter 19: RxSwiftExt............................................ 364
Chapter 20: Action..................................................... 370
Chapter 21: RxGesture............................................. 375
Chapter 22: RxRealm ................................................ 380
Chapter 23: RxAlamofire......................................... 385
Section VI: Putting It All Together ................................ 389
Chapter 24: MVVM with RxSwift......................... 390
Chapter 25: Building a Complete RxSwift
App.................................................................................... 413
Conclusion.............................................................................. 440
raywenderlich.com 7
RxSwift - Reactive Programming with Swift
Table of Contents: Extended
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
What you need. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Who this book is for. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
How to use this book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
What’s in store. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Book source code and forums. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Book updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
About the cover. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Section I: Getting Started with RxSwift . . . . . . . . . . . . . 22
Chapter 1: Hello, RxSwift! . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Introduction to asynchronous programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Foundation of RxSwift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
App architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
RxCocoa. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Installing RxSwift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Community. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Chapter 2: Observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
What is an observable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Lifecycle of an observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Subscribing to observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Disposing and terminating. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Creating observable factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Using Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
raywenderlich.com 8
RxSwift - Reactive Programming with Swift
Chapter 3: Subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
What are subjects?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with publish subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Working with behavior subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Working with replay subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Working with relays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Chapter 4: Observables & Subjects in Practice . . . . . 85
Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using a subject in a view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Talking to other view controllers via subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Creating a custom observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
RxSwift traits in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Completable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Challenges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Section II: Operators & Best Practices. . . . . . . . . . . . . 106
Chapter 5: Filtering Operators . . . . . . . . . . . . . . . . . . . . 108
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Ignoring operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Skipping operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Taking operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Distinct operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Chapter 6: Filtering Operators in Practice . . . . . . . . 125
Improving the Combinestagram project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Sharing subscriptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Improving the photo selector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Trying out time-based filter operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
raywenderlich.com 9
RxSwift - Reactive Programming with Swift
Chapter 7: Transforming Operators . . . . . . . . . . . . . . . 147
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Transforming elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Transforming inner observables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Observing events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Chapter 8: Transforming Operators in Practice . . . 164
Getting started with GitFeed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Fetching data from the web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Transforming the response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Persisting objects to disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Add a last-modified header to the request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Chapter 9: Combining Operators. . . . . . . . . . . . . . . . . . 185
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Prefixing and concatenating. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Merging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Combining elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Combining elements within a sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Chapter 10: Combining Operators in Practice . . . . 207
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Preparing the web backend service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Categories view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Adding the event download service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Getting events for categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Events view controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Wiring the days selector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Splitting event downloads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
raywenderlich.com 10