0% found this document useful (0 votes)
65 views

Download ebooks file Reactive Programming with Kotlin First Edition Learn Rx with RxJava RxKotlin and RXAndroid Alex Sullivan all chapters

First

Uploaded by

funchasserrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Download ebooks file Reactive Programming with Kotlin First Edition Learn Rx with RxJava RxKotlin and RXAndroid Alex Sullivan all chapters

First

Uploaded by

funchasserrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Reactive Programming with Kotlin First Edition


Learn Rx with RxJava RxKotlin and RXAndroid Alex
Sullivan

https://textbookfull.com/product/reactive-programming-with-
kotlin-first-edition-learn-rx-with-rxjava-rxkotlin-and-
rxandroid-alex-sullivan/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Reactive Programming with RxJava Creating Asynchronous


Event Based Applications 1st Edition Tomasz Nurkiewicz Ben
Christensen
https://textbookfull.com/product/reactive-programming-with-rxjava-
creating-asynchronous-event-based-applications-1st-edition-tomasz-
nurkiewicz-ben-christensen/
textboxfull.com

Reactive Programming with Angular and ngrx: Learn to


Harness the Power of Reactive Programming with RxJS and
ngrx Extensions 1st Edition Oren Farhi (Auth.)
https://textbookfull.com/product/reactive-programming-with-angular-
and-ngrx-learn-to-harness-the-power-of-reactive-programming-with-rxjs-
and-ngrx-extensions-1st-edition-oren-farhi-auth/
textboxfull.com

Kotlin Apprentice Second Edition Beginning Programming


with Kotlin Irina Galata

https://textbookfull.com/product/kotlin-apprentice-second-edition-
beginning-programming-with-kotlin-irina-galata/

textboxfull.com

Reactive Programming with JavaScript 1st Edition Hayward


Jonathan

https://textbookfull.com/product/reactive-programming-with-
javascript-1st-edition-hayward-jonathan/

textboxfull.com
Programming Kotlin Applications Building Mobile and Server
Side Applications with Kotlin 1st Edition Brett Mclaughlin

https://textbookfull.com/product/programming-kotlin-applications-
building-mobile-and-server-side-applications-with-kotlin-1st-edition-
brett-mclaughlin/
textboxfull.com

Programming Android With Kotlin: Achieving Structured


Concurrency With Coroutines Pierre-Olivier Laurence

https://textbookfull.com/product/programming-android-with-kotlin-
achieving-structured-concurrency-with-coroutines-pierre-olivier-
laurence/
textboxfull.com

RxSwift Reactive Programming with Swift 1st Edition


Florent Pillet

https://textbookfull.com/product/rxswift-reactive-programming-with-
swift-1st-edition-florent-pillet/

textboxfull.com

RxSwift Reactive Programming with Swift Third Edition


Raywenderlich Tutorial Team

https://textbookfull.com/product/rxswift-reactive-programming-with-
swift-third-edition-raywenderlich-tutorial-team/

textboxfull.com

Scala Reactive Programming Build scalable functional


reactive microservices with Akka Play and Lagom 1st
Edition Rambabu Posa
https://textbookfull.com/product/scala-reactive-programming-build-
scalable-functional-reactive-microservices-with-akka-play-and-
lagom-1st-edition-rambabu-posa/
textboxfull.com
Reactive Programming with Kotlin

Reactive Programming with Kotlin


By Alex Sullivan

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
Reactive Programming with Kotlin

Dedications
"To my wonderful partner Pallavi, without whom I would have never
been able to start this undertaking. Your support and encouragement
mean the world to me."

— Alex Sullivan

raywenderlich.com 3
Reactive Programming with Kotlin

About the Author


Alex Sullivan is an author of this book. Alex is a mobile developer
who works at Thoughtbot in Boston, where he enjoys reactive
programming, experimenting with different programming languages,
and tinkering with fun approaches to building mobile applications. In
his spare time, Alex enjoys traveling and relaxing with his partner,
binging unhealthy amounts of Netflix and reading. Alex hopes to one
day find a cat he's not allergic to and rant about bracket placement to
him or her.

About the Editors


Joe Howard is the final pass editor for this book. Joe is a former
physicist that studied computational particle physics using parallel
Fortran simulations. He gradually shifted into systems engineering
and then ultimately software engineering around the time of the
release of the iOS and Android SDKs. He's been a mobile software
developer on iOS and Android since 2009, working primarily at two
agencies in Boston, MA since 2011. He's now the Pillar Lead for
raywenderlich.com. Twitter: @orionthewake.

Vijay Sharma is the final pass editor of this book. Vijay is a husband,
a father and a senior mobile engineer. Based out of Canada's capital,
Vijay has worked on dozens of apps for both Android and iOS. When
not in front of his laptop, you can find him in front of a TV, behind a
book, or chasing after his kids. You can reach out to him on Twitter
@vijaysharm or on LinkedIn @vijaysharm

Manda Frederick is the editor of this book. She has been involved in
publishing for over ten years through various creative, educational,
medical and technical print and digital publications, and is thrilled to
bring her experience to the raywenderlich.com family as Managing
Editor. In her free time, you can find her at the climbing gym,
backpacking in the backcountry, hanging with her dog, working on
poems, playing guitar and exploring breweries.

raywenderlich.com 4
Reactive Programming with Kotlin

Victoria Gonda is a tech editor for this book. Victoria is a software


developer working mostly on Android apps. When she's not traveling
to speak at conferences, she works remotely from Chicago. Her
interest in tech started while studying computer science and dance
production in college. In her spare time, you can find Victoria
relaxing with a book, her partner, and her pets. You can connect with
her on Twitter at @TTGonda.

Ellen Shapiro is a tech editor for this book. Ellen is an iOS developer
for Bakken & Bæck's Amsterdam office who also occasionally writes
Android apps. She has worked in her spare time to help bring iOS
songwriting app Hum to life. She’s also developed several
independent applications through her personal company, Designated
Nerd Software. When she's not writing code, she's usually tweeting
about it at @DesignatedNerd.

Amanjeet Singh is a tech editor for this book. Amanjeet is an


Android Engineer in Delhi, India and an open source enthusiast. As a
developer he always tries to build apps with optimized performance
and good architectures which can be used on a large scale. Currently
Android Engineer at 1mg, he helps in building apps for one of the
leading healthcare platform in India. Also a technical editor and
author in android team at raywenderlich.com.

Matei Suica is a tech editor for this book. Matei is a software


developer that dreams about changing the world with his work. From
his small office in Romania, Matei is trying to create an App that will
help millions. When the laptop lid closes, he likes to go to the gym
and read. You can find him on Twitter or LinkedIn: @mateisuica

raywenderlich.com 5
Reactive Programming with Kotlin

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 6
Reactive Programming with Kotlin

Acknowldegments
We'd also like to thank the RxSwift: Reactive Programming with Swift authors, whose
work served as the basis for parts of this book:
• Scott Gardner 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.
• Junior Bontognali 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.
• Florent Pillet 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.
• Marin Todorov 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.

raywenderlich.com 7
Reactive Programming with Kotlin

Table of Contents: Overview


Book License ........................................................... 18
Book Source Code & Forums ................................... 19
What You Need ....................................................... 20
Book Updates .......................................................... 21
About the Cover....................................................... 22
Section I: Getting Started with RxJava .................... 23
Chapter 1: Hello, RxJava! .................................. 24
Chapter 2: Observables .................................... 45
Chapter 3: Subjects .......................................... 63
Chapter 4: Observables & Subjects in Practice 79
Section II: Operators & Best Practices ................... 100
Chapter 5: Filtering Operators ......................... 102
Chapter 6: Filtering Operators in Practice ....... 120
Chapter 7: Transforming Operators ................. 133
Chapter 8: Transforming Operators in
Practice ........................................................... 149
Chapter 9: Combining Operators .................... 163
Chapter 10: Combining Operators in Practice . 185
Chapter 11: Time-Based Operators ................ 203
Section III: Intermediate RxJava ............................ 223

raywenderlich.com 8
Reactive Programming with Kotlin

Chapter 12: Error Handling in Practice ............ 224


Chapter 13: Intro to Schedulers ...................... 244
Chapter 14: Flowables & Backpressure .......... 262
Chapter 15: Testing RxJava Code ................... 277
Chapter 16: Creating Custom Reactive
Extensions ...................................................... 294
Section IV: RxJava Community Cookbook ............. 311
Chapter 17: RxBindings ................................... 312
Chapter 18: Retrofit ......................................... 331
Chapter 19: RxPreferences ............................. 349
Chapter 20: RxPermissions ............................ 363
Section V: Putting It All Together ........................... 377
Chapter 21: RxJava & Jetpack ........................ 378
Chapter 22: Building a Complete RxJava App 395
Conclusion ............................................................ 423

raywenderlich.com 9
Reactive Programming with Kotlin

Table of Contents: Extended


Book License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . 19
What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Book Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
About the Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Section I: Getting Started with RxJava . . . . . . . . . . . . . 23
Chapter 1: Hello, RxJava! . . . . . . . . . . . . . . . . . . . . . . . . . 24
RxJava and RxKotlin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Introduction to asynchronous programming . . . . . . . . . . . . . . . . . . . . 26
Foundations of RxJava . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
App architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
RxAndroid and RxBinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Installing RxJava . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Chapter 2: Observables . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
What is an observable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Lifecycle of an observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Creating observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Subscribing to observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Disposing and terminating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
The create operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Creating observable factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Using other observable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

raywenderlich.com 10
Reactive Programming with Kotlin

Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Chapter 3: Subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
What are subjects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Working with publish subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Working with behavior subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Working with replay subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with async subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Working with the RxRelay library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

Chapter 4: Observables & Subjects in Practice . . . . 79


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Using a BehaviorSubject in a ViewModel . . . . . . . . . . . . . . . . . . . . . . . 81
Adding photos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Communicating with other views via subjects . . . . . . . . . . . . . . . . . . . 87
Creating a custom observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Review: Single, Maybe, Completable . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Using Single in the app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Section II: Operators & Best Practices . . . . . . . . . . . . 100


Chapter 5: Filtering Operators . . . . . . . . . . . . . . . . . . . 102
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Ignoring operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Skipping operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Taking operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Distinct operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Challenge: Create a phone number lookup . . . . . . . . . . . . . . . . . . . . . 117
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

raywenderlich.com 11
Reactive Programming with Kotlin

Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

Chapter 6: Filtering Operators in Practice . . . . . . . . . 120


Improving the Combinestagram project . . . . . . . . . . . . . . . . . . . . . . . 121
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Chapter 7: Transforming Operators . . . . . . . . . . . . . . . 133


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Transforming elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Transforming inner observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Observing events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Chapter 8: Transforming Operators in Practice . . . . 149


Getting started with GitFeed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Fetching data from the web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Transforming the response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Processing the response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Persisting objects to disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Adding a last-modified header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

Chapter 9: Combining Operators . . . . . . . . . . . . . . . . 163


Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Prefixing and concatenating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Combining elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

raywenderlich.com 12
Reactive Programming with Kotlin

Combining elements within a sequence . . . . . . . . . . . . . . . . . . . . . . . 180


Challenge: The zip case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

Chapter 10: Combining Operators in Practice . . . . . 185


Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Preparing the EONET API class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Add events into the mix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Combining events and categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Downloading in parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Wiring up the days seek bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Challenge: Adding a progress bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Chapter 11: Time-Based Operators . . . . . . . . . . . . . . 203


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Buffering operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Time-shifting operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Timer operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

Section III: Intermediate RxJava . . . . . . . . . . . . . . . . . . 223


Chapter 12: Error Handling in Practice . . . . . . . . . . . . 224
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Managing errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Handle errors with catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Catching errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Retrying on error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Errors as objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

raywenderlich.com 13
Reactive Programming with Kotlin

Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

Chapter 13: Intro to Schedulers . . . . . . . . . . . . . . . . . . 244


What is a scheduler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Setting up the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Switching schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Best practices and built-in schedulers . . . . . . . . . . . . . . . . . . . . . . . . 257
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

Chapter 14: Flowables & Backpressure. . . . . . . . . . . 262


Backpressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Buffering danger! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Natural backpressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Introduction to Flowables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Backpressure strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Flowables, Observables, Processors and Subjects — Oh, My! . . . . . 272
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Chapter 15: Testing RxJava Code . . . . . . . . . . . . . . . . 277


Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Introduction to TestObserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Using a TestScheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Injecting schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Using Trampoline scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Using subjects with mocked data. . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Testing ColorViewModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

Chapter 16: Creating Custom Reactive


Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

raywenderlich.com 14
Reactive Programming with Kotlin

Extending a framework class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296


Wiring the extension up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Wrapping the locations API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The lift and compose functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Testing your custom reactive extension . . . . . . . . . . . . . . . . . . . . . . . 307
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Section IV: RxJava Community Cookbook . . . . . . . . 311


Chapter 17: RxBindings . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Extending ValueAnimator to be reactive . . . . . . . . . . . . . . . . . . . . . . . 314
Using RxBindings with Android widgets . . . . . . . . . . . . . . . . . . . . . . . 315
Fetching colors from an API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Displaying an information dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

Chapter 18: Retrofit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Recap of Retrofit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Including Rx adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Creating a JSON object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Updating the JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Retrieving JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

Chapter 19: RxPreferences . . . . . . . . . . . . . . . . . . . . . . 349


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using SharedPreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Listening for preference updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Using RxPreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

raywenderlich.com 15
Reactive Programming with Kotlin

Subscribing to preference changes . . . . . . . . . . . . . . . . . . . . . . . . . . 356


Saving custom objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Chapter 20: RxPermissions . . . . . . . . . . . . . . . . . . . . . 363


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Requesting the location permission . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Using RxPermissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Requesting another permission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Reading from external storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Writing the weather to external storage . . . . . . . . . . . . . . . . . . . . . . . 372
Reacting to orientation changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

Section V: Putting It All Together . . . . . . . . . . . . . . . . . 377


Chapter 21: RxJava & Jetpack . . . . . . . . . . . . . . . . . . . 378
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
RxJava and Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Reacting to database changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Updating individual items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Starting the app with cached data . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Paging data in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

Chapter 22: Building a Complete RxJava App . . . . 395


Introducing QuickTodo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Architecting the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Task model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Task Dao . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Task repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Replacing callbacks with observables . . . . . . . . . . . . . . . . . . . . . . . . 409

raywenderlich.com 16
Reactive Programming with Kotlin

Editing tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412


Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

raywenderlich.com 17
L Book License

By purchasing Reactive Programming with Kotlin, you have the following license:

• You are allowed to use and/or modify the source code in Reactive Programming with
Kotlin 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
Reactive Programming with Kotlin in as many apps as you want, but must include this
attribution line somewhere inside your app: “Artwork/images/designs: from Reactive
Programming with Kotlin, available at www.raywenderlich.com”.

• The source code included in Reactive Programming with Kotlin is for your personal use
only. You are NOT allowed to distribute or sell the source code in Reactive
Programming with Kotlin 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 18
B Book Source Code &
Forums

This book comes with the source code for the starter and completed projects for each
chapter. These resources are shipped with the digital edition you downloaded from
https://store.raywenderlich.com/products/reactive-programming-with-kotlin.

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 or to submit any errors you may find.

raywenderlich.com 19
W What You Need

To follow along with the tutorials in this book, you’ll need the following:

• A PC running Windows 10 or a recent Linux such as Ubuntu 18.04 LTS, or a


Mac running the latest point release of macOS Mojave or later: You’ll need one
of these to be able to install the latest versions of IntelliJ IDEA and Android Studio.

• IntelliJ IDEA Community 2019.1 or later: IntelliJ IDEA is the IDE upon which
Android Studio is based, and it's used in the book to look at pure Kotlin projects that
demonstrate techniques in RxJava. You can download the latest version of IntelliJ
IDEA Community for free here: https://www.jetbrains.com/idea/

• JDK 8 or later: You'll need a Java Development Kit installed for use with IntelliJ
IDEA projects (Android Studio will use its own version of the JDK). You can download
the Oracle JDK from here: https://www.oracle.com/technetwork/java/javase/
downloads/index.html

• Android Studio 3.3.2 or later: Android Studio is the main development tool for
Android. You can download the latest version of Android Studio for free here:
https://developer.android.com/studio

• An intermediate level knowledge of Kotlin and Android development. This book is


about learning RxJava 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 Kotlin and the Android SDK.

All the Android sample projects in this book will work just fine in an Android emulator
bundled with Android Studio, or you can also use a physical Android device.

raywenderlich.com 20
B Book Updates

Since you’ve purchased the digital edition version of this book, you get free access to
any updates we may make to the book!

The best way to get update notifications is to sign up for our monthly newsletter. This
includes a list of the tutorials that came out on raywenderlich.com that month, any
important news like book updates or new books, and a list of our favorite iOS
development links for that month. You can sign up here:

• www.raywenderlich.com/newsletter

raywenderlich.com 21
A About the Cover

The common starling, pictured on the cover of this book, seems just that: common. It
isn't particularly large — roughly only 8 inches long. It isn't particularly musical and is
considered noisy in flocks and communal roosts. It's also not particularly beautiful,
with dark glossy feathers and a subtle metallic sheen.

And, yet, this simple bird continues to hold our attention, even being referenced in
literature as early as Shakespeare. Why?

First, it has a talent for mimicry and, like the reactive sensibilities explored in this book,
is highly responsive to its environment. It has up to 20 distinct imitations of other
birds, and it is even known to mimic ringing phones and car alarms.

And, most impressively, a flock of starlings in flight is a gorgeous display of reactivity


in motion. You've probably seen it, yourself: thousands of birds creating fluid shapes —
called murmurations — in the air, never pausing, each bird responding to the next.

While we can't know how these birds evolved to this level of cooperation and
responsiveness, we hope to draw some inspiration from them in this book as we guide
you through developing your own reactive programming.

You can learn more about these birds, here: https://en.wikipedia.org/wiki/


Common_starling.

See them in flight, here: https://video.nationalgeographic.com/video/short-film-


showcase/00000158-457d-d0be-a1dc-4f7f8e650000.

raywenderlich.com 22
Section I: Getting Started with
RxJava

In this part of the book, you’re going to learn about the basics of RxJava. You are going
to have a look at what kinds of asynchronous programming problems RxJava 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 1: Hello, RxJava!

Chapter 2: Observables

Chapter 3: Subjects

Chapter 4: Observables & Subjects in Practice

raywenderlich.com 23
1 Chapter 1: Hello, RxJava!
By Alex Sullivan & Marin Todorov

This book aims to introduce you, the reader, to the RxJava, RxKotlin and RxAndroid
libraries and to writing reactive Android apps with Kotlin.

RxJava and RxKotlin


You may be asking yourself "Wait, why am I reading about RxJava when I'm using Kotlin
to build Android apps?" Great question! RxJava has been around since 2013, well before
Kotlin began to be accepted as a mainstream programming language, and is part of a
long list of Rx-based libraries written for different platforms and systems. Since Kotlin
has such excellent interopability with Java, it wouldn't make sense to completely
rewrite RxJava for Kotlin — you can just use the existing RxJava library instead!

However, just because RxJava doesn't need to be completely rewritten to work in Kotlin
doesn't mean that it couldn't benefit from all of the great features in the Kotlin
programming language.

That's where RxKotlin comes into play. RxKotlin is a library that expands RxJava by
adding a ton of utilities and extension methods that make working with RxJava much
more pleasant in Kotlin. However, you absolutely do not need RxKotlin to use the
RxJava library in a Kotlin-based Android app.

But what exactly is RxJava? Here’s a good definition:

RxJava is a library for composing asynchronous and event-based code by using


observable sequences and functional style operators, allowing for parameterized
execution via schedulers.

raywenderlich.com 24
Reactive Programming with Kotlin Chapter 1: Hello, RxJava!

Sound complicated? Don’t worry if it does. Writing reactive programs, understanding


the many concepts behind them and navigating a lot of the relevant, commonly used
lingo might be intimidating — especially if you try to take it all in at once, or when you
haven’t been introduced to it in a structured way.

That’s the goal of this book: to gradually introduce you to the various RxJava APIs and
Rx concepts by explaining how to use each of the APIs, and then covering their
practical usage in Android apps.

You’ll start with the basic features of RxJava, 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 RxJava 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 RxJava is though, have we? 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.

RxJava, in its essence, simplifies developing asynchronous programs by allowing your


code to react to new data and process it in a sequential, isolated manner. In other
words, RxJava lets you observe sequences of asynchronous events in an app and
respond to each event accordingly. Examples are taps by a user on the screen and
listening for the results of asynchronous network calls.

As an Android app developer, this should be much more clear and tell you more about
what RxJava 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 RxJava helps you write
asynchronous code. And you know that developing good, deterministic, asynchronous
code is hard, so any help is quite welcome!

raywenderlich.com 25
Reactive Programming with Kotlin Chapter 1: Hello, RxJava!

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 Android app, at any moment, might be doing any of the following things and more:

• Reacting to button taps

• Animating a view across the screen

• Downloading a large photo from the internet

• Saving bits of data to disk

• Playing audio

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. Android offers
you several different 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 determine
which piece of code updates the data first or which code has read the latest value.

Android asynchronous APIs


Google has provided several different APIs that help you write asynchronous code.
You've probably used a few of them before, and chances are they left you leaving a bit
frustrated or maybe even scared.

You’ve probably used at least one of the following:

• AsyncTask: To do some work on the background and then update elements in your UI
with the result of that background work. You have to make sure to properly handle
cancelling a running AsyncTask when your Activity or Fragment shuts down, since
you could otherwise get a NullPointerException when the AsyncTask tries to update
UI elements that don't exist anymore.

raywenderlich.com 26
Reactive Programming with Kotlin Chapter 1: Hello, RxJava!

• IntentService: To start a fire-and-forget background job using an Intent. You


typically use an IntentService if you want to do some work that doesn't need to
touch the UI at all — saving an object to a database, for example.

• Thread: To start background work in a purely Java way without interacting with any
Android APIs. Threads come with the downside of being expensive and not bound to
any sort of ThreadPool.

• Future: To clearly chain work which will complete at some undetermined point in
the future. Futures are considerably clearer to use than AsyncTasks, but run into
some of the same problems around null pointers when a Fragment or Activity has
been destroyed.

The above isn't an exhaustive list — there's also Handler, JobScheduler, WorkManager,
HandlerThread and Kotlin coroutines.

Coroutines and RxJava


Now that Kotlin coroutines have started to become popular in the Android
development world, you may be asking yourself if it's still worthwhile to learn about
RxJava.

Many comparisons have been made between using RxJava and using coroutines for
Android development. Each review will give you a different answer about which tool
you should use.

In reality, RxJava and coroutines work at different levels of abstractions. Coroutines


offer a more lightweight approach to threading and allow you to write asynchronous
code in a synchronous manner. Rx, on the other hand, is used primarily to create the
event-driven architecture mentioned above, and to allow you to write reactive
applications. So, while they both offer an answer for doing asynchronous work off the
main thread, they're really different tools that are both useful depending on the
context.

If you're simply looking for an easy way to replace AsyncTask, then coroutines may
make more sense than pulling RxJava into your application. However, if you do want to
move towards a reactive, event-driven architecture, then RxJava is your best bet!

Asynchronous programming challenges


Since most of your typical classes would do something asynchronously, and all UI
components are inherently asynchronous, it’s impossible to make assumptions in what
order the entirety of your app code will get executed.

raywenderlich.com 27
Reactive Programming with Kotlin Chapter 1: Hello, RxJava!

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,
there's a litany of tools — like the ones listed above — that Android developers have
been using to write asynchronous apps since well before RxJava hit the scene.

The issue is that complex asynchronous code becomes very difficult to write in part
because of the variety of APIs that you as an Android developer will end up using:

You may be using an AsyncTask to update your UI, an IntentService to save something
to a database, a WorkManager task to sync your app to a server, and other various
asynchronous APIs. 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.

Synchronous code
Performing an operation for each element of a list is something you’ve done plenty of
times. It’s a very simple yet solid building block of app logic because it guarantees two
things: It executes synchronously, and the collection is immutable from the outside
world while you iterate over it.

raywenderlich.com 28
Reactive Programming with Kotlin Chapter 1: Hello, RxJava!

Take a moment to think about what this implies. When you iterate over a collection,
you don’t need to check that all elements are still there, and you don’t need to rewind
back in case another thread inserts an element at the start of the collection. You
assume you always iterate over the collection in its entirety at the beginning of the loop.

If you want to play a bit more with these aspects of the for loop, try this in an app or
IntelliJ IDEA project:

var list = listOf(1, 2, 3)


for (number in list) {
println(number)
list = listOf(4, 5, 6)
}
print(list)

Is list mutable inside the for body? Does the collection that the loop iterates over ever
change? What’s the sequence of execution of all commands? Can you modify number if
you need to? You may be surprised by what you see if you run this code.

Asynchronous code
Consider similar code, but assume each iteration happens as a reaction to a click on a
button. As the user repeatedly clicks on the button, the app prints out the next element
in a list:

var list = listOf(1, 2, 3)


var currentIndex = 0
button.setOnClickListener {
println(list[currentIndex])

if (currentIndex != list.lastIndex) {
currentIndex++
}
}

Think about this code in the same context as you did for the previous one. As the user
clicks the button, will that print all of the list's elements? You really can’t say. Another
piece of asynchronous code might remove the last element, before it’s been printed.

Or another piece of code might insert a new element at the start of the collection after
you’ve moved on.

Also, you assume only that the click listener will ever change currentIndex, but another
piece of code might modify currentIndex as well — perhaps some clever code you
added at some point after crafting the above function.

You’ve likely realized that some of the core issues with writing asynchronous code are:
a) the order in which pieces of work are performed and b) shared mutable data.

raywenderlich.com 29
Exploring the Variety of Random
Documents with Different Content
“It is small, as you see,” remarked Mrs. Colonel Norcott deprecatingly;
“but I hope you’ll be comfortable. House-rent is uncommonly dear in
London in the season—and the Colonel likes to have everything
comfortable—and so much expense coming on, you know.”
She spoke the last words in a kind of confidential whisper, throwing a
degree of playful significance over the implied suggestion. Honor, who did
not know, but who felt that there was a dessus les cartes, could only look
with a slightly puzzled air at her mysterious hostess; whereupon that lady,
with what was intended for a pretty air of confusion, said:
“Another time, when we know each other better, being both matrons—
there need be no secrets between us. Ah, I see, there is hot water—the girl
of the house is generally so careless—you will come down when you are
ready. The Colonel is particular about his hours: so don’t be long, there’s a
dear. The Colonel always goes to his club after dinner, and you wouldn’t
like to keep him waiting. Can I do anything for you? No. Well, then, I will
leave you to beautify;” and so saying, Mrs. Norcott betook herself to her
own, namely, the adjoining and equally unprepossessing-looking sleeping
chamber.
Honor sat down on the edge of her narrow iron bedstead, and felt for the
moment like one dazed. All that during the last hour had occurred was so
widely different from her previous imaginings. Where was the sick-room,
the wasted invalid, the atmosphere of physic-bottles and of gloom? Instead,
had she not been received with chaff and cheerfulness? Was not her father
in apparently perfect health? And already, simultaneously almost with her
arrival, had not a visitor appeared, an intimacy with whom her conscience
warned her had better be avoided, and regarding whom, as she was well
aware, her mother-in-law had already thrown out hints as uncalled for as
they were offensive? And then, closely following on these mental
communings, there came the more searching questions of, “What would
John think of all this mean deceit? Would he not, in his indignation at the
trick, instantaneously require her return?” Instantaneously? Yes! Honor felt
well assured of that; John Beacham was the last man in the world to suffer
patiently such a deception as had been practised upon him. He hated—of
that too his wife was well aware—the man who had deceived him, with as
much of rancorous feeling as he was capable of entertaining; and therefore
any further sojourn on her part under her father’s roof would, were John to
become cognisant of the truth, have been indignantly protested against. But,
on the other hand, Honor herself, though aware that she had been lured
under a false presence to Stanwick-street, was by no means disposed to
disclose this fact to one so deeply interested as was John in all that so nearly
concerned her safety and well-being. She was conscious, on the contrary, of
a very decided inclination to remain, for the present at least, where she was.
Of course this inclination—the mere passing of the desire through her heart,
to say nothing of the yielding to it—was wrong, selfish, and unwifely to the
most unpardonable extent. Honor, too, was well aware that she was guilty
in not driving from her, at the very first onslaught, the tempter who assailed
her. There was little excuse, save in the weakness of our most imperfect
human nature, for the taking of this first dearly-bought step in wrong. It cost
but little trouble putting that wrong foot foremost. She had but to be
passive, nothing could be easier, and the affair, one might almost say,
righted itself. But facile as it all had seemed, and scarcely out of the
common order of things, the time came when that poor weak woman would
have given all of life that remained to her, could she have, by so doing,
annulled the decision of a moment, and thus averted all the terrible
consequences that followed thereupon.
She would not write—so Honor, with very little hesitation, decided—to
inform John Beacham that her father was well in health, and that her longer
stay in London was uncalled for. It was wonderful, whilst acting thus, how
many specious arguments she made use of to persuade herself that she was
less guilty than she seemed. No one could imagine—so she told herself
(sitting before the small painted mirror, with her fair hair hanging loosely
about her neck and shoulders)—no one could by any possibility imagine
that she could be there for her own pleasure. Such a wretched room as they
had given her! Room, indeed! Would anyone in his or her senses at
Peartree-house have called that closet of a place by such an inappropriate
name? And then her father, though his bodily health was acknowledged to
be satisfactory, yet betrayed, in Honor’s opinion, evident symptoms of a
mind ill at ease at least, if not diseased. His pecuniary affairs, too, struck his
inexperienced daughter as being in no flourishing condition. The smallness
and mesquinerie of the house (a part of which only was occupied by her
relations); the absence of a man-servant—for Honor had picked up a good
deal of the knowledge of “life” from the pages of three-volume novels; to
say nothing of the dress, cheap though showy, of her hostess, betrayed to
Honor the fact that either money was far less plentiful in her father’s than
her husband’s home, or that penuriousness was a vice indulged in to a large
extent among her newly-found connections.
“If my father were rich and prosperous,” she said to herself—and, to do
her only justice, this ingenious though unconscious sophist firmly believed
in the honesty of her excuses—“if my father were rich and prosperous, I
should act differently, and write at once to tell John he is well. I should
mind in that case less, I think, the making him have a still worse opinion of
my poor father than I know he has at present. If I could hope that John
would make excuses for them, I should know better what to do; but he
would be simply furious. I know that he would never have let me come, if
he had not believed that my father was suffering from the effects of that
horrid blow; and perhaps, after all, he was; and besides, if he did make the
worst of his illness for the purpose of having me with him, where was the
mighty harm? It only shows that my poor father loves me,” added Honor,
sadly, to the wilful tender little heart, which was, alas, so likely to be led
astray by its own warm womanly impulses.
It was with such false reasonings as these, that Honor persuaded herself
to keep the real state of things a secret, pro tem. at least, from her husband.
He would never, she decided—even if he lived to the age of Methuselah—
understand her feelings, or see things (even if she wrote to him about her
father) as she saw them; and so, after sighing a little over her husband’s
small amount of solicitude and comprehension regarding the trifling things
that so much make or mar the happiness of a young and childless woman,
Honor entered on her course of deception. She was interrupted in her
cogitations—cogitations which so materially affected the future happiness
of one who, albeit he wore his heart, as the saying is, on his sleeve, Honor
had proved herself to be so little capable of comprehending—by a hasty
knock from the “girl of the house”—the “young person,” whose back hair
was thrust into a greasy net, and whose upper woman was clothed in a red
Garibaldi that had evidently seen service, which warned the visitor from the
country that by indulging herself in reflection, she was doing the wrong
thing at the wrong time. Dinner was “on table,” as the parlour-maid (to
whom Colonel Fred was in the habit of saying civil things) with no great
show of respect informed her; and Honor, in some trepidation—for she did
not feel exactly at home in her father’s house—hastily put the finishing
touches to her simple toilette, and hurriedly, two steps at a time, in very
unmatronly fashion, descended the many flights of stairs to the drawing-
room. Colonel Norcott was standing on the hearthrug with his back to the
fireless grate, when his daughter entered with an apology on her lips, and a
pretty deprecating smile lighting up her face. Mrs. Norcott had already
taken her seat at the round table, which had been cleared for dinner, and
was gazing with large anxious eyes, anticipative of evil, on the pewter dish-
cover, not one of the brightest specimens of its kind, that graced the simple
board.
“I am so sorry,” Honor was beginning; “I had my trunk to unpack; and
my hair was so tumbled that I was obliged to undo it, and—”
“And you couldn’t have been quicker if you’d done it for a bet,” said the
Colonel good humouredly. “So now for the miserable meal we call dinner.
You’ll wish yourself back at the farm-house, I suspect, when you taste the
nastiness that Mrs. Thingummy treats us with. You get rather different grub
at home—eh, Miss Honor?”
His daughter laughed lightly as she took her place beside him. It amused
her greatly to be called “Miss Honor,” just as if she were a girl. And then he
looked at, and spoke so kindly to her that she was already beginning to feel
at home in the Stanwick-street lodging. With the large-boned, youthfully-
dressed matron, dispensing with an air that was intended to be genteel, the
ill-dressed, London-flavoured whiting from the scantily-filled dish before
her, Honor did not expect to feel much kindred sympathy. Mrs. Norcott
was, however, to judge from external appearances at least, good-natured,
and facile à vivre. If she had ever enjoyed the prestige of being that often
self-reliant and arrogant character yclept an heiress (a fact of the truth of
which Honor began to doubt), there had as yet cropped out no signs either
of a love of domination or a purse-proud spirit. A harmless vanity, joined to
a blind worship for the Colonel, had hitherto struck Honor as the most
distinguishing feature in the Australian lady’s idiosyncrasy.
“If you can eat that stuff it’s more than I can,” said the master of the
house, pushing away his plate with disgust. “You see, my dear child, what it
is to have a father who hasn’t one shilling to rub against another. I—”
“Now, Colonel, I am surprised to hear you talk in this way,” put in his
wife. “There are a precious good many shillings in three hundred pounds a-
year, or I am a good deal more out in my arithmetic than I think I am. The
idea of talking in this way before your daughter! Why, she’ll think she’s
come among beggars, to hear the way you’re going on.”
Fred laughed sardonically. “Not much need to talk about it, I think.
Those horrible whitings fried in black grease render all further explanation
on the subject nugatory. I don’t suppose that any affectation of superfluity is
likely to deceive Mrs. Beacham; and, in my opinion, it is always better to be
plain-spoken. You’ve got a poor devil of a father, my dear child, who finds
it hard enough, I can assure you, to make both ends meet. Of course you
will be uncomfortable here, I expect that; but I do flatter myself that you
won’t throw me over, Honor, because I’m a poor man. I’ve trusted in
women all my life, and never had cause to repent it yet; so here’s your
health, my dear, and may you enjoy health and wealth and happiness long
after your poor old father has been laid under the sod.”
He had taken advantage of Lydia’s momentary absence to utter this
pathetic speech, and as the red Garibaldi was not there to mar by force of
contrast the Colonel’s paternal platitudes, he got through his toast
swimmingly. In another moment, and before Honor’s hand, which had been
lovingly extended to meet her father’s, could be withdrawn, the parlour-
maid, in whose roguish black eyes the “first-floor front” was certainly no
hero, had bounced back into the room, bearing before her a large specimen
of that economical and succulent dish known to housekeepers as a juicy leg
of mutton. Nothing overcome at the sight of this delicacy, Mrs. Norcott
pressed a slice, cut with the gravy in, on Honor’s acceptance.
“What, not a mouthful? Dear me now, how sorry I am! Is there nothing
we could tempt you with? It’s because the mutton’s raw, perhaps, that your
stomach turns a little at it. Lydia, can you get nothing from the larder for
Mrs. John Beacham? That knuckle of ham, now—”
Miss Lydia grinned broadly. “You’ll never see that there mouthful of ’am
again, ’m,” she said pertly, and after the fashion of one accustomed to speak
her mind. “The Kunnle he ate that for his breakfast this morning afore he
went out;” and having so said, she went on briskly with the important duties
of her calling.
To Honor, accustomed as she was to “the land flowing with milk and
honey” of the old farm-house at Updown Paddocks, the state of the
Stanwick-street larder appeared a most deplorable affair indeed. As for her
father—her high-bred, distinguished-looking father, with his delicate
aristocratic hands, his dainty golden sleeve-buttons, and, in her opinion, his
warm paternal heart—she could hardly refrain from tears as she looked
upon his futile efforts to eat the nauseous food that was set before him. She
had not been reared, as we know, in the school of over-refinement, and to
do violence to her own feelings in order to spare the self-love of another
was one of the consequences on an advanced state of civilisation which had
not, as yet, made itself felt in the somewhat arrière parish of Switcham. To
feign an appetite if she had it not was not therefore amongst the small
deceptions which Mrs. Beacham felt called upon to practise, and for that
reason the poor girl rose dinnerless, or, as she would in her ignorance have
called it, supperless, from that untempting board. No sooner had the
energetic Lydia retired, closing the door upon all (save its unwelcome
perfume) that remained of that highly-unsatisfactory repast, than Colonel
Norcott, taking his hat from a side table, announced his intention of going
out.
“Only for an hour or so—just to smoke a cigar in the open,” he said
carelessly. “You’ll be in bed though, Honor, I suppose, before I come back?
Beauty-sleep, eh? We mustn’t lose those country roses sooner than we can
help, or we shall have John looking us up with that stout stick of his. Gad,
how quick he struck, and how it tingled! I can feel it now;” passing his hand
playfully over his forehead. “Took one so deucedly by surprise, you know.
Hadn’t an idea, of course, that he was going to do anything of the kind.
Nothing but a light cane in my hand, talking quietly about old times, when,
without a word, down comes the sledge-hammer, and, by Jove! I was
floored.”
“He was very sorry afterwards, indeed he was,” Honor said pleadingly.
“I have heard him say so often. He hardly knew what he did. If he had not
been sorry, he would never have let me leave the Paddocks to-day.”
“Wouldn’t he?” chucking her under the chin. “Looks sharp after his
pretty wife, eh? But as to being sorry, I can believe as much of that as I like.
If he had been, he would have answered a letter that I wrote to him some
time ago about Rough Diamond; but as he didn’t,” and a very vindictive
expression flitted across his bearded face, “I know what to think, and, what
is more, shall probably act upon the conviction I have come to.”
Honor could only look her surprise at this wholly unexpected outburst.
Before, however, she could utter a syllable in extenuation of her husband’s
sin against politeness, Colonel Norcott had taken his departure, leaving her
to spend the hours till welcome bedtime came in listening to the
uncongenial gossip of the woman whose society was by himself so
evidently unprized. Honor’s first experience of genteel life in London was
certainly neither an amusing nor an instructive one.
CHAPTER XXII.

THE TURNING OF THE HEAD.


“How pretty Rhoda is looking! Arthur, I am sure, in spite of what you say,
that she will make a sensation in London. Her features are so regular, and
her complexion, though she is pale, is so wonderfully clear and pure.
Besides she looks so good, so—”
“She had better look bad,” growled old Mr. Duberly, in response to his
daughter’s sisterly remarks on the personal recommendations of the
debutante from Sandyshire. “She had far better look as wicked as the evil
one himself if she wants to nobble the young Lunnon gents. A gell who
‘looks good’ has no more chance of what you call getting on there than my
Soph,” and he winked at his daughter facetiously, “would have had, if she
hadn’t happened to have a few thousand pounds in her pocket, poor gell!
There’s that Lady Fy, as they call her, with her dead eyes and painted hair; I
wonder where she would be if looking good was the order of the day, and if
whalebone didn’t make the woman, and—”
“And want of it the stick,” laughed Arthur. “But you are quite right, sir,
and Sophy is making a great mistake about Rhoda; she won’t have a chance
in London. The ugliest girl going, if she were only chaffy and got herself up
well, would cut her out at once, knock her into smithereens, if she set about
having a try with poor Rhoda. And then the entourage, the home, I
mean”—explaining the foreign word for the benefit of his father-in-law,
who understood about as much of the French language as he did of that of
the twelve tribes—“the home in which my unfortunate sisters live is the last
one in the world to tempt a man to commit matrimony in their behalf. In my
opinion there is nothing like a jolly, genial mamma—the kind of mamma
exactly that milady is not—for getting girls well married. By the bye,
Sophy, my child—now don’t agitate yourself, or we may have to send
prematurely for Mrs. Gamp—Lady Mill intends doing you the honour of
calling here, and—”
“O, Arthur, don’t say so! How horrid! I would so much rather go there.
It is so much nicer to be able to go away when one has had enough—when
one has nothing more to say, I mean, and—and I know I shall hate it so.”
Old Dub, who formed a component part, and a greatly valued one by his
daughter, of Arthur Vavasour’s family, had left the room the moment that
the conversation turned, which it often did, on Lady Millicent and her
shortcomings. The good old man, conscious of harbouring (a very
unwelcome guest) in his inner man a decidedly unchristian dislike to the
lady in question, abstained, as far as lay in his power, from the gratification
—for such it undoubtedly was—of hearing her abused and ridiculed. His
sense of honour, too, which was singularly keen, revolted from any secret
attacks, any stabs in the dark, any—even the most well deserved—
accusations, when the individual so accused was, from absence or
ignorance, deprived not only of the opportunity of self-justification, but of
bringing en evidence the “other side of the question.”
“You’ll be so good, Master Arthur,” he had once remarked, and that in
his most decided manner, to his thoughtless son-in-law, “as not to say
anything in my presence about your mother which you wouldn’t say before
her face. I don’t pretend to much liking for milady, nor do I suppose there’s
much love lost between us. She thinks me an old snob (isn’t that your new
word for fogeyism?) and I—well, it don’t much matter what I think. I’m an
old fellow, and an old-fashioned fellow, and in my time—don’t laugh, you
rogue—young men and women honoured their fathers and mothers in a way
they don’t seem to do nowadays. You may say that the parents don’t always
deserve to be respected; but that’s neither here nor there, and God A’mighty
said nothing about that,” added the old man reverently, “when He gave the
two tables of the law to Moses. One of these days, my boy,” and he laid his
hand kindly upon Arthur’s shoulder, “you’ll be glad if your own sons have
been brought up in my way of thinking. In the mean time, remember that
it’s a bargain between us that you are to be mum in my presence about my
Lady Mill.”
But we must return to Mrs. Arthur Vavasour, and to her anything but
joyful anticipations as regarded the expected visit of her uncongenial
mother-in-law.
“O Arthur, what shall I do? I could bear anything better than a formal
call from Lady Millicent. What am I to say to her? She has such a
dreadfully cold way of looking into and at one. She shows so plainly that
she despises papa and me. I don’t mind it so much for myself, for of course
I know that you might have married anybody; but it does vex me when she
behaves so about poor papa. I know he feels it, though he says nothing.
Papa is so odd, dear old man, about some things; he is so chivalrous, so
innately courtly, like the old knights. Don’t laugh, Arthur; I know he does
not look much like them, with his round face and dear bald head; but I
think, I do indeed, that they, when they fought for their ladies, must have
felt something like my poor father. He places woman so high. I can
remember how courteous and tender—though I daresay Lady Millicent
thinks him vulgar—he always was to poor mamma. He has a horror of fast
girls; they are the only creatures in the shape of women that I ever hear him
severe upon; but he would love Rhoda dearly. O Arthur, how nice it would
be if we could see a good deal of her! But of course Lady Millicent would
never allow that; she would think her daughter would be contaminated by
living with such vulgarians as papa and me.”
Arthur during this plaintive speech had been standing with his back to
the speaker, tapping impatiently on the plate-glass window-pane, having a
view upon the now tolerably crowded park, for it was five o’clock, and Mr.
Duberly’s grand town mansion was one of the finest in Hyde-park-gardens.
He turned round hastily at the cessation of his wife’s voice, and said a little
impatiently:
“How silly you are, dear, to vex yourself in this way! Because my
mother happens to be a vulgar fine lady—and believe me, Sophy pet, that
no genuine lady (the kind of lady that your friends the knights worshipped),
is ever rude—because my mother happens not to know how to behave
herself, it behoves you to give her a lesson.”
“Give her a lesson! Me!” exclaimed Sophy, in amazement.
“Yes, you! Why not? Only be your own dear, merry, unaffected self, only
show that you, tremendous heiress that you are (and which I never can get
you to remember), are not to be put down and tyrannised over and
browbeaten, and, believe me, Lady Millicent, like all bullies, will draw in
her horns, and treat you with the respect that is your due.”
“I don’t want respect,” sighed Sophy; “all I long for is affection. I should
so like your mother to love me, Arthur. Just now, too,” and she blushed
prettily, “I feel more than ever reminded that I have no mother of my own.”
“And no great loss, either, if all mothers are like the only one that I have
any experience of,” said Arthur lightly, while endeavouring to divert his
wife’s thoughts into a more lively channel. “But, Sophy darling, if it bores
you too much, you sha’n’t see milady. There is no positive necessity for it. I
can easily say that Williams has ordered you to be quiet, and” (as he settled
the cushion behind her shoulders, for Sophy was delicate, and both liked
and required attention) “if you like it, I will stay and protect you, read to
you, do what you like, dear, as your cold is to keep you at home this
disagreeable day.”
But to this sacrifice, for sacrifice she well knew it was, on her husband’s
part, Sophy would not listen. He was the very best, the kindest, and the
dearest, so, with grateful tears and smiles, she told him, of created beings.
The idea of his giving up his ride for her! Who but himself would have
dreamt of such an act of self-devotion? As regarded Lady Millicent’s visit,
too, it was so very thoughtful, so nice of him to let her off. But Sophy could
be self-sacrificing as well as he; so, though she felt weak and languid, she
assured her hero that there was no necessity for either care or quiet, that she
felt quite equal to the threatened visitation, and intended on that very day,
provided that Lady Mill kept her promise of calling, to practise Arthur’s
lesson on behaviour.
A bright happy smile lighted up her face as Arthur, kissing her forehead
lightly, said a few farewell words. Nor did the look of placid contentment
fade away even after the music of his foot upon the stair had ceased to echo
in her ears; for the young wife was wonderfully happy—as happy, perhaps,
as it is ever given to mortal woman here below to feel. Assured of her
husband’s affection, and adoring him with all the force of her warm young
heart; with not a wish ungratified, and endowed with health and spirits to
enjoy the good gifts that were with such a lavish hand bestowed upon her;
blest with a kind father’s doting tenderness, and dreaming as she lay on her
luxuriant couch of the “prime of bliss” in a few weeks to be accorded to her
prayers, who can wonder that Arthur Vavasour’s wife was one of the
happiest of her sex?
And he, the man who was so seldom absent from her thoughts, the
husband, to obey whose slightest wish was felt by the unselfish Sophy to be
a blessed privilege which all must envy her, did he, as he strode forth
hastily, for he had an appointment and was late, feel no remorseful pang for
wandering thoughts, and dreams on joys forbidden? I fear that to this query
I must answer in the negative. It is not in the earliest stages of our cherished
sins that the avenging demon of unavailing regret rises up before us, and
embitters joys that else were sweet. Sin, the very wickedness that is
destined to be our ruin and shame, assumes at the beginning such very fair
and false proportions. In early sunlight the shadows are so short, the path
before so bright, the glare so dazzling and the day so long. It is when the
shadows lengthen, and the night approaches, when the sin which in the
distance looked so unlike crime stands revealed a hideous skeleton, bereft
of all its false adornments, in our walk by day and in our couch by night,
that we look back to the past with misery and vain remorse, asking
ourselves the bitter, futile question whether the gain is worthy of the cost,
while like withered leaves in the sad autumn time,
“The hopes of our youth fall thick in the blast,
And the days are dark and dreary.”

Arthur Vavasour, although he might not be exactly in that blissful


condition which has been described as having “nothing on one’s mind,” was
nevertheless quite sufficiently light-hearted as he wended his way that
spring afternoon in the direction of Stanwick-street, to make all things
around and about him seem wonderfully bright and hopeful. The “tropic
time of youth,” the possession of life’s choicest gifts—health, wealth, and
beauty—were enough in themselves to account for the “gleams elysian”
that lighted up this young man’s path while, treading as though it were air
the Tyburnian pavement, he wended his way to the presence of his peerless
Honor. His!—for already he had begun to arrogate to himself the monopoly
of his neighbour’s wife. The monopoly, that is to say, of her time, her
thoughts, her liking. Already there could, it is to be feared, be little doubt of
the melancholy fact, that the affection and fidelity of his own wife would, in
comparison with that of honest John’s, have been but little prized. Of the
one he was assured, while over the other hung just enough of the delightful
veil woven of hope and imagination to lend enchantment to the prospect
which his own unrestrained passions, aided by the machinations of one of
the vilest of human beings, held up before him.
To the motives as well as to the plottings of Honor’s unprincipled father,
the infatuated young man resolutely closed his eyes. He could scarcely fail
to see that Colonel Norcott was ready to promote and encourage his
intimacy with John Beacham’s wife, and under any other circumstances
Arthur would have turned aside with disgust and horror from conduct so
vile and so unprincipled. But we are very apt (and Vavasour was no
exception to the rule) to condone the sin by which we hope to profit; and
there were besides other reasons, although I must not anticipate, which
would assist in accounting to the reader for the readiness with which the
young heir of Gillingham fell into the plans and projects of the almost
universally “held cheap” sporting-man.
On this occasion, the occasion of his ostensible visit to the master of the
house, he found, as he expected, Honor Beacham alone. That she was
agitated by his appearance both her words and the pretty confusion which
she was far too untutored to disguise, betrayed; and that Arthur, forgetful of
his young wife at home—forgetful of old ties of friendship, and utterly
careless of consequences as regarded the guileless wife whose peace he was
undermining—made the best of the passing hours allowed him by the
prudential arrangements of the unprincipled Fred, who that knows anything
of the worst side of human nature can doubt?
Before Arthur Vavasour took his leave he had thoroughly succeeded in
awakening in Honor Beacham’s ambitious and pleasure-loving breast a
keen desire to partake of such gaieties and amusements as he might be able
to procure for her. On the following morning a riding-habit, borrowed from
the wardrobe of the unsuspicious Sophy, was to be altered (if necessary) for
Mrs. Beacham’s use; a box at the Opera was to be placed at her disposal,
and everything worth either seeing or doing was to be seen and done by the
pretty recluse of Updown Paddocks. Honor knew that it was all wrong; but
her head was beginning to be turned, and that night as she laid it on her
pillow she almost went the length of congratulating herself that she had
kept the unsophisticated John in the dark regarding her father’s health. She
had every chance of catching a glimpse of the ineffable delights of London
in the season, as those delights were imagined in her foolish dreams, and
coloured by the man whose words had from the first moment of her
acquaintance with him possessed such a strange and unfortunate influence
over her still childish mind.
CHAPTER XXIII.

THE RECTOR COMMITS HIMSELF.


Arthur was not far wrong when he averred that his sister Rhoda was the
last girl in the world to make a successful début in London society. Shyness
—being entirely uncomprehended and believed in—is too often there
mistaken for stupidity, or, at best, for a disinclination to be pleased and to be
“jolly.” Looking beneath the surface is too long a process for the superficial
observers of the present day; summary condemnation takes little time, and
lynch-law, in the shape of something very like ostracism, is speedily
pronounced. So Rhoda Vavasour—whose mother had been guilty of that
terrible social blunder, namely, the non-keeping up of her connections—
found herself, although rather pretty and decidedly “nice,” quite thrown
away in the West-end world. Under these circumstances, and feeling—
gentle though she was, and humble-minded—a little mortified and
délaissée, Rhoda’s heart naturally turned again towards the man who, being
but a country rector, looked up to, loved, and appreciated her. For her—
almost partnerless, and left, whether at ball, opera, or party, a good deal to
her own resources and reflections—the memory of George Wallingford,
together with her pleasant country duties and avocations, was as a gleam of
summer sunshine, a green oasis in the peopled desert of her uncongenial
life. When, before her departure from Gillingham, the rector, meeting her
by chance as she was leaving the door of a poor dependent on her kindness,
stopped his horse, and by a few meaning words, and looks more suggestive
still, conveyed to her the knowledge that neither time nor absence would
erase her image from his heart, he of course committed this disloyal act
with eyes wide open to its impropriety. He knew, ay better than did Arthur
Vavasour, that he was, in thus secretly tampering with a young girl’s
affections, committing a sin against his neighbour. That sin might not,
viewed abstractedly, be so heinous as that which Rhoda’s brother was, with
equal audacity, committing; but in reality the one man’s guilt was little
inferior to the other; both yielded to such temptation as was within their
reach, and the consequence in either case was the punishment that had been
so deservedly incurred.
Lady Millicent, whose motives for passing the season in London could
scarcely be called motherly ones, troubled herself little about her elder
daughter’s pallid cheeks and languid movements.
“Rhoda was always pasty,” was her answer to Lady Guernsey, when that
kind friend endeavoured to draw her attention to the small amount of
interest taken by the débutante in the amusements that were offered to her;
“Rhoda never had more colour than a boiled fowl. And in London I don’t
see how she is to get rosier; unless, indeed,” correcting herself a little
spitefully, “she were to employ the same tricks that other girls do; and that,
I need not tell you, is not in Rhoda’s line.”
“I cannot help fancying,” Lady Guernsey said after a pause, during
which she was wondering a little nervously how her hint would be taken,
—“I cannot help fancying that Rhoda may have taken a fancy, may have
seen someone who—”
Lady Millicent drew herself up indignantly. “I should have imagined,”
she said, “that you had seen enough of Rhoda, enough too of my mode of
bringing up, to render such a surmise—ahem—impossible. My daughter
has never left my roof but once; and that, my dear Lady Guernsey, was
when you kindly invited her to Gawthorpe. I should be distressed to think
that while with you, and under Lord Guernsey’s protection, she should have
met with anyone who, I mean—pray allow me to proceed—anyone who
had clandestinely gained her affections, and thus put a bar—for I could not
tolerate anything underhand—to my receiving him as a son-in-law.”
Lady Guernsey could not restrain a smile at this indignant outburst
against one who was in point of fact a myth. Preparing to rise from the sofa,
on which the two chaperones had taken refuge from the crowd and heat of a
“delightful” ball, she said courteously:
“You must forgive me for my guess, and believe that nothing but our
deep interest in dear Rhoda could have called it forth. As to her having seen
anyone in the shape of a lover at Gawthorpe, that, I can assure you, was out
of the question. Anything more dull than we were last Christmas, you
would hardly believe. The death of poor Guernsey’s sister made gaiety out
of the question; but the children, all things considered, seemed to enjoy
themselves; and I am only grieved to see your girl looking so dull and
altered.”
She put out her hand to her country neighbour as she spoke, and Lady
Millicent, whose thoughts had already wandered away to law-courts, will-
cases, and appeals, returned her goodnight with apparent cordiality.
Meanwhile—for absence, as is universally acknowledged, is apt to
increase les grandes passions, while it has a directly contrary effect upon
minor ones—meanwhile the rector of Switcham, in that pretty but dull
study of his, sat ruminating, sometimes by the hour together, on the
perfections of Rhoda Vavasour; on their mutual attachment (for George
Wallingford felt as assured of the young girl’s love as if she had told him
the sweet truth in words), and on the maddening possibility that in London,
in the midst of the gay and exciting scenes of which he had heard and read
so much, some man less faint of heart or more attractive than himself, might
step between him and his love, and thus deprive him of all that made life
dear and valuable. The idea that such might, by some terrible contingency,
be the case, was, as I have just said, very distracting to the rector; so
distracting, that his usually well-balanced mind tottered a little on its throne,
and the judgments and resolves—so calm habitually, and prudent—of
sensible George Wallingford, gave token of the emotion that was going on
within his breast.
That it would be an act little short of insanity to request of Lady
Millicent Vavasour the hand of her daughter in marriage, Rhoda’s clerical
lover would, no later than three days before, have been quite willing to
acknowledge; but, whether it is true that quos Deus vult perdere prius
dementat, or that the man as well as “the woman who deliberates is lost,” it
is needless to inquire, the fact remaining the same—namely, that poor
George Wallingford, believing his position could not be worse, and might
by some strange freak of Fortune be bettered by his desperate act, arrived at
the somewhat rash resolution of proposing himself to Lady Millicent
Vavasour as the future husband of her daughter.
The determination was no sooner arrived at than it was acted upon.
Alone with his own thoughts, his own fears, and the hopes which the last
tender smile which he had seen on Rhoda’s quivering lip had raised within
his heart, the unfortunate young rector, unable to endure suspense, rushed
madly to his fate. Alas for him, poor fellow!—where was at that crisis of
his life the outspoken, sensible, hard-headed college friend, who with a
strong grip—painful, perhaps, but salutary—would have plucked him from
the edge of the abyss, and with a “Don’t make a fool of yourself, old
fellow!” would have given wholesome counsel, and have staved off much
of the unhappiness soon to be recorded?
The letter on which—as he touchingly explained to the sympathising
Lady Millicent—the happiness of his future life depended was written just
four days after the departure of the Castle family for London. Under what
circumstances it was received, how Lady Millicent behaved on its perusal,
and the consequences resulting from its perpetration, will form subject-
matter for another chapter.
CHAPTER XXIV.

FAST DOWNHILL.
Honor’s first letter to her husband—which she despatched the day but one
after she left the Paddocks—was not written without a considerable amount
of thought and caution. It was not in this young creature’s nature to be false.
On the contrary, deceit hung like a chain of lead upon her mind—a chain
that was heavy and galling, and which, save when she was diverted from
the pain it caused her by the excitement of dissipation, of Arthur Vavasour’s
society, and, alas, by the admiration of which this true daughter of Eve well
knew herself to be the object, Honor’s consciousness of her own disloyalty
caused her to feel very thoroughly unhappy and remorseful. Her letter to
John—the first which she had ever addressed to him—caused her even
while she wrote it to blush for shame. In it—while she carefully avoided, or
rather ingeniously glossed over, the subject of her father’s illness—any
acute observer might have perceived that she was in no hurry to return to
Sandyshire. She had found the Colonel better, she wrote; but he was
looking ill, and complained a good deal of his head. (This, in Honor’s
defence, I may remark, was no invention on her part.) Altogether, she felt
she was a comfort to her father. He was in very different circumstances
from what she (Honor) had imagined him to be. Really quite poor, she
almost thought. They had no house, only a small lodging, and they lived
very poorly—so poorly that Honor was quite sure John would not like it at
all; while as for mother she would not be able to exist on the kind of food
that was served up in Stanwick-street. As regarded herself, she was—
though she could bear it very well for a time—extremely uncomfortable. A
little attic at the very top of the house!—an attic with a sloping roof, and no
closet to hang her dresses in, and—But there is no occasion to follow Mrs.
John in her filling up of this her first letter with diatribes against her own
discomfort, and with comparisons (flattering, as she hoped, to the old lady’s
pride) between the wretchedness of the furniture and the poverty-stricken
nature of the food in Stanwick-street, and the luxuries and comforts which
she, the writer, had enjoyed at home. She wound up her letter (a missive in
which she had carefully abstained from any allusion to the amusements,
both present and to come, which were in readiness for her delectation) with
a hope that her father’s health would continue to improve; in which case, in
a fortnight or ten days, perhaps, Colonel Norcott would let her talk of
returning to the Paddocks. Both he and his wife showed her so much
kindness and affection that, however much she would like to be at home
again, she could not think just at present of speaking about her return.
This was the sum and substance of Honor’s letter to her husband, and
now that it has been very frankly laid before the reader, I greatly fear that
his opinion of my poor heroine will not be much raised thereby. Little
enough have I to say in her excuse. She was very human, very womanly;
with the seeds of corruption in her veins, the child of a weak mother and a
wicked father, her infant innocence unshielded by a prayer, the silent
nursery work, the holy secret duties which are a mother’s province, and hers
only to perform, perforce left undone—how can we wonder then that this
child of nature, abandoned to the care of hirelings, and so early transplanted
to a soil in many respects uncongenial, should prove herself to be no better
than hundreds upon hundreds of her sex and age, who, giving up reality for
things hoped for, find to their cost that their foolish discontent has proved
their deadliest bane!
“Well, John, and what is the letter about? She seems to have written
plenty, any way; but I suppose you won’t care to tell me what all that heap
of writing is about;” and Mrs. Beacham, who, as was usual with her at
letter-delivery time, was busily employed in the housewifely and popular
task of concocting strong tea from a not over liberal allowance of the herb,
endeavoured to look as if she were not in the slightest degree interested in
the queries to which she had just given voice.
John, who had just finished his letter, and was proceeding to read it a
second time, in order to render himself thoroughly master of its somewhat
puzzling contents, pushed the delicate and young-lady-like looking epistle
across the table with rather an impatient gesture.
“I don’t know that you’ll be able to make much more of a hand of it than
I can,” he said gruffly, for his heart felt terribly sore, and with men of John
Beacham’s stamp emotion does not manifest itself in an especially tender
fashion. “The man hasn’t much the matter with him, as far as I can
understand; never had, I daresay,” he added under his breath, but his mother
caught the words, and treasured them in her mind for future use.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like