RxSwift Reactive Programming with Swift Third Edition Raywenderlich Tutorial Team instant download
RxSwift Reactive Programming with Swift Third Edition Raywenderlich Tutorial Team instant download
https://textbookfull.com/product/rxswift-reactive-programming-
with-swift-third-edition-raywenderlich-tutorial-team/
https://textbookfull.com/product/rxswift-reactive-programming-
with-swift-1st-edition-florent-pillet/
https://textbookfull.com/product/design-patterns-by-tutorials-
third-edition-learning-design-patterns-in-swift-raywenderlich-
tutorial-team/
https://textbookfull.com/product/ios-apprentice-eighth-edition-
beginning-ios-development-with-swift-and-uikit-raywenderlich-
tutorial-team/
https://textbookfull.com/product/ios-11-by-tutorials-learning-
the-new-ios-apis-with-swift-4-1st-edition-the-raywenderlich-
tutorial-team/
Server Side Swift with Vapor Building Web APIs and Web
Apps in Swift 3rd Edition Raywenderlich.Com Tutorial
Team
https://textbookfull.com/product/server-side-swift-with-vapor-
building-web-apis-and-web-apps-in-swift-3rd-edition-
raywenderlich-com-tutorial-team/
https://textbookfull.com/product/concurrency-by-tutorials-second-
edition-multithreading-in-swift-with-gcd-and-operations-tutorial-
team/
https://textbookfull.com/product/machine-learning-by-tutorials-
beginning-machine-learning-for-apple-and-ios-first-edition-
raywenderlich-tutorial-team/
https://textbookfull.com/product/data-structures-algorithms-in-
kotlin-implementing-practical-data-structures-in-kotlin-1st-
edition-raywenderlich-tutorial-team/
https://textbookfull.com/product/reactive-programming-with-
javascript-1st-edition-hayward-jonathan/
RxSwift - Reactive Programming with Swift
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
— Florent Pillet
"For my grandfather."
— Junior Bontognali
"For Charlotte"
— Scott Gardner
raywenderlich.com 3
RxSwift - Reactive Programming with Swift
raywenderlich.com 4
RxSwift - Reactive Programming with Swift
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.
raywenderlich.com 5
RxSwift - Reactive Programming with Swift
raywenderlich.com 7
RxSwift - Reactive Programming with Swift
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
raywenderlich.com 9
RxSwift - Reactive Programming with Swift
raywenderlich.com 10
RxSwift - Reactive Programming with Swift
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
raywenderlich.com 11
RxSwift - Reactive Programming with Swift
raywenderlich.com 12
RxSwift - Reactive Programming with Swift
raywenderlich.com 13
RxSwift - Reactive Programming with Swift
raywenderlich.com 14
RxSwift - Reactive Programming with Swift
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
raywenderlich.com 15
I Introduction
"If you've ever used an asynchronous callback based API, you've probably dealt with
handling the response data ad-hoc all across your codebase, and have most likely
decided there was no way to unit test it all... But, let me tell you - there is a better way,
and it's called Rx!"
— Krunoslav Zaher, creator of RxSwift
There’s no denying it: Rx is one of the hottest topics in mobile app development these
days!
If you visit international conferences, or even local meetups, it might feel like everyone
is talking about observables, side effects, and (gulp) schedulers.
The RxSwift library (part of the larger family of Rx ports across platforms and
languages) allows you to use your favorite Swift programming language in a completely
new way. The somewhat difficult-to-handle asynchronous code in Swift becomes much
easier and a lot saner to write with RxSwift.
raywenderlich.com 16
RxSwift - Reactive Programming with Swift Introduction
In this book, you’ll learn how RxSwift solves the issues related to asynchronous
programming and master various reactive techniques, from observing simple data
sequences, to combining and transforming asynchronous value streams, to designing
the architecture and building production quality apps.
By the end of this book, you’ll have worked through the chapter content and you’ll have
hands-on experience solving the challenges at the end of the chapters — and you’ll be
well on your way to coming up with your own Rx patterns and solutions!
• A Mac running the latest point release of OS X High Sierra or later: You’ll need
this to be able to install the latest version of Xcode.
• Xcode 10 or later: Xcode is the main development tool for iOS. You can download
the latest version of Xcode for free on the Mac app store here: https://
itunes.apple.com/app/xcode/id497799835?mt=12
• An intermediate level knowledge of Swift and iOS development. This book is about
learning RxSwift specifically; to understand the rest of the project code and how the
accompanying demo projects work you will need at least an intermediate
understanding of Swift and UIKit.
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 the paid developer
account is completely optional.
raywenderlich.com 17
RxSwift - Reactive Programming with Swift Introduction
If you’re a complete beginner to iOS, we suggest you first read through the latest
edition of the iOS Apprentice. That will give you a solid foundation of building iOS apps
with Swift from the ground up but you might still need to learn more about
intermediate level iOS development before you can work through all chapters in this
book.
If you know the basics of iOS development but are new to Swift, we suggest you read
through Swift Apprentice first, which goes through the features of Swift using
playgrounds to teach the language.
http://store.raywenderlich.com
Other chapters provide you with a starter project that includes some non-Rx logic
inside and lead you through Rx-ifying the project by adding code in key places. In the
process, you’ll see what difference RxSwift makes in the project code and how to
approach different common problems.
We do suggest that you work through the chapters in order, since the concepts build
upon each other. Remember you’ll get the most out of the book if you follow along with
the tutorials and perform the hands-on challenges.
For advanced developers, there’s still value in the early chapters since they cover the
basics. However if you’re comfortable with those concepts, feel free to jump ahead to
the topics that interest you the most.
raywenderlich.com 18
RxSwift - Reactive Programming with Swift Introduction
What’s in store
This book is divided into six sections. You can find more details on each section in its
introduction. Here’s a brief overview.
raywenderlich.com 19
RxSwift - Reactive Programming with Swift Introduction
We’ve also set up an official forum for the book at forums.raywenderlich.com. This is a
great place to ask questions about the book, discuss debugging strategies or to submit
any errors you may find.
Book updates
Great news: since you purchased the PDF version of this book, you’ll receive free
updates of the book’s content!
The best way to receive update notifications is to sign up for our weekly newsletter. This
includes a list of the tutorials published on raywenderlich.com in the past week,
important news items such as book updates or new books, and a few of our favorite
developer links. Sign up here:
• www.raywenderlich.com/newsletter
License
By purchasing RxSwift: Reactive Programming in Swift, you have the following license:
• You are allowed to use and/or modify the source code in RxSwift: Reactive
Programming in 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
RxSwift: Reactive Programming in Swift in as many apps as you want, but must include
this attribution line somewhere inside your app: “Artwork/images/designs: from
RxSwift: Reactive Programming in Swift book, available at http://
www.raywenderlich.com.”
• This book is for your personal use only. You are NOT allowed to sell this book in
whole or in part without prior authorization, or distribute it to friends, co-workers or
students; they would need to purchase their own copy.
raywenderlich.com 20
RxSwift - Reactive Programming with Swift Introduction
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 non-infringement. 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 book are the property of
their respective owners.
Its size however, is not what should worry you, should you have the chance to meet one
in person. That little devil sports a natural taser gun that can discharge up to 860 volts
and 1 ampere of current! (Yeah, I hear you. Where was that electric eel when you were
trying to jump-start your car that morning last February, right?)
The electric eel was chosen for the Rx project logo since it's pre-release code name was
Volta. Now you know!
raywenderlich.com 21
Section I: Getting Started with
RxSwift
In this part of the book, you’re going to learn about the basics of RxSwift. You are going
to have a look at what kinds of asynchronous programming problems RxSwift
addresses, and what kind of solutions it offers.
Further, you will learn about the few basic classes that allow you to create and observe
event sequences, which are the foundation of the Rx framework.
You are going to start slow by learning about the basics and a little bit of theory. Please
don't skip these chapters! This will allow you to make good progress in the following
sections when things get more complex.
Chapter 2: Observables
Chapter 3: Subjects
raywenderlich.com 22
1 Chapter 1: Hello, RxSwift!
By Marin Todorov
This book aims to introduce you, the reader, to the RxSwift library and to writing
reactive iOS apps with Swift.
raywenderlich.com 23
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!
That’s the goal of this book: to gradually introduce you to the various RxSwift APIs and
Rx concepts by explaining how to use each of the APIs, and then by covering their
practical usage in iOS apps.
You’ll start with the basic features of RxSwift, and then gradually work through
intermediate and advanced topics. Taking the time to exercise new concepts extensively
as you progress will make it easier to master RxSwift by the end of the book. Rx is too
broad of a topic to cover completely in a single book; instead, we aim to give you a solid
understanding of the library so that you can continue developing Rx skills on your own.
We still haven’t quite established what RxSwift is though, have we? Let’s start with a
simple, understandable definition and progress to a better, more expressive one as we
waltz through the topic of reactive programming later in this chapter.
As an iOS app developer, this should be much more clear and tell you more about what
RxSwift is, compared to the first definition you read earlier in this chapter.
Even if you’re still fuzzy on the details, it should be clear that RxSwift helps you write
asynchronous code. And you know that developing good, deterministic, asynchronous
code is hard, so any help is quite welcome!
Introduction to asynchronous
programming
If you tried to explain asynchronous programming in a simple, down to earth language,
you might come up with something along the lines of the following.
An iOS app, at any moment, might be doing any of the following things and more:
• Playing audio
raywenderlich.com 24
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!
All of these things seemingly happen at the same time. Whenever the keyboard
animates out of the screen, the audio in your app doesn’t pause until the animation has
finished, right?
All the different bits of your program don’t block each other’s execution. iOS offers you
various kinds of APIs that allow you to perform different pieces of work on different
threads and perform them across the different cores of the device’s CPU.
Writing code that truly runs in parallel, however, is rather complex, especially when
different bits of code need to work with the same pieces of data. It’s hard to argue about
which piece of code updates the data first, or which code read the latest value.
• The delegate pattern: Lets you define an object that acts on behalf, or in
coordination with, another object. For example, in your app delegate, you define
what should happen when a new remote notification arrives, but you have no idea
when this piece of code will be executed or how many times it will execute.
raywenderlich.com 25
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!
• Grand Central Dispatch: To help you abstract the execution of pieces of work. You
can schedule code to be executed sequentially in a serial queue, or run a multitude of
tasks concurrently on different queues with different priorities.
• Closures: To create detached pieces of code that you can pass around in your code,
so other objects can decide whether to execute it or not, how many times, and in
what context.
Since most of your typical code would perform some work asynchronously, and all UI
events are inherently asynchronous, it’s impossible to make assumptions in what order
the entirety of your app code will get executed.
After all, your app’s code runs differently depending on various external factors, such as
user input, network activity, or other OS events. Each time the user fires up your app,
the code may run in a completely different order depending on those external factors.
(Well, except for the case when you have an army of robots testing your app, then you
can expect all events to happen with precise, kill-bot synchronization.)
We’re definitely not saying that writing good asynchronous code is impossible. After all,
the great APIs from Apple listed above are very advanced, very specialized for the task
and, to be fair, quite powerful compared to what other platforms offer.
The issue is that complex asynchronous code becomes very difficult to write in part
because of the variety of APIs that Apple’s SDK offers:
Using delegates requires you to adopt one particular pattern, another one for closures,
yet another approach for subscribing to NotificationCenter, and so on. Since there is
no universal language across all the asynchronous APIs, reading and understanding the
code, and reasoning about its execution, becomes difficult.
To wrap up this section and put the discussion into a bit more context, you’ll compare
two pieces of code: one synchronous and one asynchronous.
raywenderlich.com 26
Exploring the Variety of Random
Documents with Different Content
bank and many of the drooping branches of the trees are
submerged; the natives approach quietly in their canoes, cautiously
push the net under the submerged vegetation, and then with a
sudden jerk lift it up out of the water, in this way capturing numbers
of small fish which had been sheltering or looking for food among
the leaves.
Another form of fishing net—though there is no netting in its
construction—is made of long, thin strips of bamboo tied parallel to
each other at intervals of about half an inch, forming a sort of screen
or trellis-work, which can be rolled up if necessary. Strong wooden
stakes are driven into the mud at the mouth of the creeks which join
the river in many places, and at high water the screens are fastened
to the stakes in such a way as to touch the bottom and close the
entrance of the creek; the water can run back when the tide falls,
but not the fish which are sometimes caught in considerable
numbers.
The larger fish are all obtained by the men, who either catch them
with a hook and line, or spear them in the shallow water near the
river mouth, or along the sea shore. We saw very few hooks; one or
two were made of rough metal, the others were neatly fashioned
from fish bones, and all of them were plain without barbs. Now they
have a large number of steel fishhooks, which they greatly value.
The commonest types of fish-spear are made of thin bamboo or a
light wood about ten feet long, and they end in three or four sharp
prongs of bamboo or hardened wood. They also use a barbed spear
of which the head becomes detached from the shaft, when it
becomes fixed in a fish; a light line connecting the shaft with the
head causes the shaft to act as a drag on the movements of the fish,
which can easily be followed up and killed; this kind of spear is only
used for the larger fish, saw-fish and the like, but I never saw it in
use. Considering the enormous number of fish that there are—at the
mouth of the river the water is sometimes seen to be seething with
large fish—it cannot be said that the men are very clever with their
spears.
They also shoot fish, using single- or three-pointed arrows; you may
see a man standing quietly in a pool of water like a heron waiting for
the fish to come up to him, or stalking a shoal of fish stealthily from
the bank; in either case he will probably shoot arrow after arrow
without effect, for they are absurdly indifferent marksmen with the
bow.
The most primitive methods of all of catching fish I saw practised
one day coming down from Obota. A native paddling in the bow of
my canoe saw a large fish near the bank, towards which he steered
the canoe. When he judged that he was near enough to it, he hurled
himself flat on to the water with a resounding splash that drenched
everything in the boat, and a thud that would have stunned the fish
at once had it not darted off an instant earlier.
The sight of a fish, however small it is, always rouses a Papuan to
action. When we were travelling with natives, we sometimes came
to pools where small fish had been left by some receding flood.
Instantly their loads were thrown down and everyone darted into the
water with sticks and stones and shouts and as much enthusiasm as
if the fish had been salmon and a full meal for everyone.
There is another method of fishing which was observed by the
navigator, Captain Dampier, in use by the natives of this region. It is
so remarkable that, although we did not see it employed by the
people of the Mimika district, I shall make no excuse for repeating it
here:—
“They strike Fish very ingeniously with Wooden Fiss-gigs and have a
very ingenious way of making the Fish rise: For they have a piece of
Wood curiously carv’d and painted much like a Dolphin (and perhaps
other Figures;) these they let down into the Water by a Line with a
small weight to sink it; when they think it low enough, they haul the
Line into their Boats very fast, and the Fish rise up after this Figure;
and they stand ready to strike them when they are near the Surface
of the Water.”11
There are times when the natives get more fish than they know
what to do with, and other times when no fish can be caught; but
they have no idea of laying up a store for the lean times. It is true
that they char some in the fire and keep them for a few days before
the fish putrify, but if they learnt to smoke some of their surplus
supply, they need never go hungry.
CHAPTER X
Food of the Papuans—Cassowaries—The Native Dog—Question of Cannibalism—
Village Headman—The Social System of the Papuans—The Family—Treatment
of Women—Religion—Weather Superstitions—Ceremony to avert a Flood—The
Pig—A Village Festival—Wailing at Deaths—Methods of Disposal of the Dead—
No Reverence for the Remains—Purchasing Skulls.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com