0% found this document useful (0 votes)
11 views60 pages

RxSwift Reactive Programming with Swift Third Edition Raywenderlich Tutorial Team instant download

The document provides information about the book 'RxSwift: Reactive Programming with Swift' and its authors, along with links to download various related educational resources. It includes a detailed table of contents outlining the structure of the book, covering topics from getting started with RxSwift to advanced concepts and community contributions. Additionally, it mentions the rights, liabilities, and trademarks associated with the book's content.

Uploaded by

ybtpyldmkc425
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)
11 views60 pages

RxSwift Reactive Programming with Swift Third Edition Raywenderlich Tutorial Team instant download

The document provides information about the book 'RxSwift: Reactive Programming with Swift' and its authors, along with links to download various related educational resources. It includes a detailed table of contents outlining the structure of the book, covering topics from getting started with RxSwift to advanced concepts and community contributions. Additionally, it mentions the rights, liabilities, and trademarks associated with the book's content.

Uploaded by

ybtpyldmkc425
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/ 60

RxSwift Reactive Programming with Swift Third

Edition Raywenderlich Tutorial Team download

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

Download full version ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

RxSwift Reactive Programming with Swift 1st Edition


Florent Pillet

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

Design Patterns by Tutorials Third Edition Learning


Design Patterns in Swift Raywenderlich Tutorial Team

https://textbookfull.com/product/design-patterns-by-tutorials-
third-edition-learning-design-patterns-in-swift-raywenderlich-
tutorial-team/

iOS Apprentice Eighth Edition Beginning iOS Development


with Swift and UIKit Raywenderlich Tutorial Team

https://textbookfull.com/product/ios-apprentice-eighth-edition-
beginning-ios-development-with-swift-and-uikit-raywenderlich-
tutorial-team/

iOS 11 by Tutorials Learning the new iOS APIs with


Swift 4 1st Edition The Raywenderlich Tutorial Team

https://textbookfull.com/product/ios-11-by-tutorials-learning-
the-new-ios-apis-with-swift-4-1st-edition-the-raywenderlich-
tutorial-team/
Server Side Swift with Vapor Building Web APIs and Web
Apps in Swift 3rd Edition Raywenderlich.Com Tutorial
Team

https://textbookfull.com/product/server-side-swift-with-vapor-
building-web-apis-and-web-apps-in-swift-3rd-edition-
raywenderlich-com-tutorial-team/

Concurrency by Tutorials Second Edition Multithreading


in Swift with GCD and Operations Tutorial Team

https://textbookfull.com/product/concurrency-by-tutorials-second-
edition-multithreading-in-swift-with-gcd-and-operations-tutorial-
team/

Machine Learning by Tutorials Beginning Machine


Learning for Apple and iOS First Edition Raywenderlich
Tutorial Team

https://textbookfull.com/product/machine-learning-by-tutorials-
beginning-machine-learning-for-apple-and-ios-first-edition-
raywenderlich-tutorial-team/

Data Structures Algorithms in Kotlin Implementing


Practical Data Structures in Kotlin 1st Edition
Raywenderlich Tutorial Team

https://textbookfull.com/product/data-structures-algorithms-in-
kotlin-implementing-practical-data-structures-in-kotlin-1st-
edition-raywenderlich-tutorial-team/

Reactive Programming with JavaScript 1st Edition


Hayward Jonathan

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

RxSwift: Reactive Programming with Swift


Florent Pillet, Junior Bontognali, Marin Todorov & Scott Gardner

Copyright ©2019 Razeware LLC.

Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.

Notice of Liability
This book and all corresponding materials (such as source code) are provided on an “as
is” basis, without warranty of any kind, express of implied, including but not limited to
the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in the
software.

Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.

raywenderlich.com 2
RxSwift - Reactive Programming with Swift

Dedications
"To my father. To my mom. To Mirjam and our beautiful daughter."

— Marin Todorov

"For Fabienne and Alexandra"

— Florent Pillet

"For my grandfather."

— Junior Bontognali

"For Charlotte"

— Scott Gardner

raywenderlich.com 3
RxSwift - Reactive Programming with Swift

About the Authors


Florent Pillet is an author of this book. Florent has been developing
for mobile platforms since the last century and moved to iOS on day
1. He adopted reactive programming before Swift was announced and
has been using RxSwift in production since 2015. A freelance
developer, Florent also uses Rx on Android and likes working on tools
for developers like the popular NSLogger when he's not contracting
for clients worldwide. Say hello to Florent on Twitter at @fpillet.

Junior Bontognali is an author of this book. Junior has been


developing on iOS since the first iPhone and joined the RxSwift team
in the early development stage. Based in Switzerland, when he's not
eating cheese or chocolate, he's doing some cool stuff in the mobile
space, without denying to work on other technologies. Other than
that he organizes tech events, speaks and blogs. Say hello to Junior on
Twitter at @bontoJR.

Marin Todorov is an author of this book. Marin is one of the


founding members of the raywenderlich.com team and has worked on
seven of the team's books. Besides crafting code, Marin also enjoys
blogging, teaching, and speaking at conferences. He happily open-
sources code. You can find out more about Marin at
www.underplot.com.

Scott Gardner is an author of this book. Scott has been developing


iOS apps since 2010, Swift since the day it was announced, and
RxSwift since before version 1. He's authored several video courses,
tutorials, and articles on iOS app development, presented at
numerous conferences, meetups, and online events, and this is his
second book. Say hello to Scott on Twitter at @scotteg.

raywenderlich.com 4
RxSwift - Reactive Programming with Swift

About the Editors


Ash Furrow is the technical editor of this book. Ash is a Canadian
iOS developer and author, currently working at Artsy. He has
published a number of books, built many apps, and is a contributor to
the open source community. On his blog ashfurrow.com, he writes
about a range of topics, from interesting programming to
explorations of analogue film photography.

Chris Belanger is the editor of this book. Chris Belanger is the Book
Team Lead and Lead Editor for raywenderlich.com. If there are words
to wrangle or a paragraph to ponder, he‘s on the case. When he kicks
back, you can usually find Chris with guitar in hand, looking for the
nearest beach, or exploring the lakes and rivers in his part of the
world in a canoe.

Shai Mishali is the final pass editor of this book. He's iOS Tech Lead
for Gett, the on-demand mobility company, and is involved in several
open source projects on his spare time - mainly the
RxSwiftCommunity and RxSwift projects, as well as an international
speaker.As an avid enthusiast of hackathons, Shai took 1st place at
BattleHack Tel-Aviv 2014, BattleHack World Finals San Jose 2014, and
Ford's Developer Challenge Tel-Aviv 2015. You can find him on
GitHub and Twitter as @freak4pc.

About the Artist


Vicki Wenderlich is the designer and artist of the cover of this book.
She is Ray’s wife and business partner. She is a digital artist who
creates illustrations, game art and a lot of other art or design work for
the tutorials and books on raywenderlich.com. When she’s not
making art, she loves hiking, a good glass of wine and attempting to
create the perfect cheese plate.

raywenderlich.com 5
RxSwift - Reactive Programming with Swift

Table of Contents: Overview


Introduction ............................................................................. 16
Section I: Getting Started with RxSwift ........................ 22
Chapter 1: Hello, RxSwift!.......................................... 23
Chapter 2: Observables .............................................. 45
Chapter 3: Subjects ....................................................... 69
Chapter 4: Observables & Subjects in Practice . 85
Section II: Operators & Best Practices ........................ 106
Chapter 5: Filtering Operators .............................. 108
Chapter 6: Filtering Operators in Practice ....... 125
Chapter 7: Transforming Operators .................... 147
Chapter 8: Transforming Operators in
Practice ........................................................................... 164
Chapter 9: Combining Operators ......................... 185
Chapter 10: Combining Operators in
Practice ........................................................................... 207
Chapter 11: Time-Based Operators .................... 227
Section III: iOS Apps with RxCocoa.............................. 246
Chapter 12: Beginning RxCocoa ........................... 247
Chapter 13: Intermediate RxCocoa .................... 268
Section IV: Intermediate RxSwift/RxCocoa.............. 290
raywenderlich.com 6
RxSwift - Reactive Programming with Swift

Chapter 14: Error Handling in Practice.............. 291


Chapter 15: Intro to Schedulers............................ 311
Chapter 16: Testing with RxTest ........................... 326
Chapter 17: Creating Custom Reactive
Extensions ...................................................................... 342
Section V: RxSwift Community Cookbook ................ 358
Chapter 18: Table & Collection Views ................ 359
Chapter 19: RxSwiftExt ............................................ 364
Chapter 20: Action ..................................................... 370
Chapter 21: RxGesture............................................. 375
Chapter 22: RxRealm ................................................ 380
Chapter 23: RxAlamofire ......................................... 385
Section VI: Putting It All Together ................................ 389
Chapter 24: MVVM with RxSwift......................... 390
Chapter 25: Building a Complete RxSwift
App .................................................................................... 413
Conclusion .............................................................................. 440

raywenderlich.com 7
RxSwift - Reactive Programming with Swift

Table of Contents: Extended


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
What you need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Who this book is for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
How to use this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
What’s in store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Book source code and forums. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Book updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
About the cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Section I: Getting Started with RxSwift . . . . . . . . . . . . . 22


Chapter 1: Hello, RxSwift! . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Introduction to asynchronous programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Foundation of RxSwift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
App architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
RxCocoa. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Installing RxSwift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Community. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Chapter 2: Observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
What is an observable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Lifecycle of an observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Subscribing to observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Disposing and terminating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Creating observable factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Using Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

raywenderlich.com 8
RxSwift - Reactive Programming with Swift

Chapter 3: Subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
What are subjects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with publish subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Working with behavior subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Working with replay subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Working with relays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

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


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using a subject in a view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Talking to other view controllers via subjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Creating a custom observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
RxSwift traits in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Completable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

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


Chapter 5: Filtering Operators . . . . . . . . . . . . . . . . . . . . 108
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Ignoring operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Skipping operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Taking operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Distinct operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

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


Improving the Combinestagram project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Sharing subscriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Improving the photo selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Trying out time-based filter operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

raywenderlich.com 9
RxSwift - Reactive Programming with Swift

Chapter 7: Transforming Operators . . . . . . . . . . . . . . . 147


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Transforming elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Transforming inner observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Observing events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Chapter 8: Transforming Operators in Practice . . . 164


Getting started with GitFeed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Fetching data from the web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Transforming the response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Persisting objects to disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Add a last-modified header to the request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

Chapter 9: Combining Operators . . . . . . . . . . . . . . . . . . 185


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Prefixing and concatenating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Combining elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Combining elements within a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

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


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Preparing the web backend service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Categories view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Adding the event download service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Getting events for categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Events view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Wiring the days selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Splitting event downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

raywenderlich.com 10
RxSwift - Reactive Programming with Swift

Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

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


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Buffering operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Time-shifting operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Timer operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Section III: iOS Apps with RxCocoa . . . . . . . . . . . . . . . . 246


Chapter 12: Beginning RxCocoa . . . . . . . . . . . . . . . . . . . 247
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Using RxCocoa with basic UIKit controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Binding observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Improving the code with Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Disposing with RxCocoa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

Chapter 13: Intermediate RxCocoa . . . . . . . . . . . . . . . 268


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Showing an activity while searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Extending CCLocationManager to get the current position . . . . . . . . . . . . . 272
How to extend a UIKit view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
One more thing: A signal! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Conclusions about RxCocoa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

Section IV: Intermediate RxSwift/RxCocoa . . . . . . . 290


Chapter 14: Error Handling in Practice . . . . . . . . . . . . 291
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Managing errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Handle errors with catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Catching errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

raywenderlich.com 11
RxSwift - Reactive Programming with Swift

Retrying on error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297


Custom errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Advanced error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Chapter 15: Intro to Schedulers . . . . . . . . . . . . . . . . . . . 311


What is a scheduler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Setting up the project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Switching schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Best practices and built-in schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

Chapter 16: Testing with RxTest . . . . . . . . . . . . . . . . . . . 326


Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Testing operators with RxTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Testing RxSwift production code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

Chapter 17: Creating Custom Reactive


Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
How to create extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Using custom wrappers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Testing custom wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Common available wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357

Section V: RxSwift Community Cookbook. . . . . . . . . 358


Chapter 18: Table & Collection Views . . . . . . . . . . . . . 359
Basic table view. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Multiple cell types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

raywenderlich.com 12
RxSwift - Reactive Programming with Swift

Providing additional functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362


RxDataSources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Chapter 19: RxSwiftExt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364


unwrap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
distinct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
mapAt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
filterMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
retry and repeatWithBehavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
catchErrorJustComplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
pausable and pausableBuffered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
bufferWithTrigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
withUnretained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
mapMany . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

Chapter 20: Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370


Creating an Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Connecting buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Composing behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Passing work items to cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Manual execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Perfectly suited for MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Chapter 21: RxGesture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375


Attaching gestures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Supported gestures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Advanced usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Chapter 22: RxRealm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380


Auto-updating results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Asynchronous first item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Changesets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Single objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

raywenderlich.com 13
RxSwift - Reactive Programming with Swift

Adding objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384


Deleting objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

Chapter 23: RxAlamofire . . . . . . . . . . . . . . . . . . . . . . . . . . 385


Basic requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Request customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Response validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Downloading files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Upload tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Tracking progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

Section VI: Putting It All Together . . . . . . . . . . . . . . . . . 389


Chapter 24: MVVM with RxSwift. . . . . . . . . . . . . . . . . . 390
Introducing MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Getting started with Tweetie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Optionally getting access to Twitter's API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Finishing up the network layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Adding a View Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Adding a View Model test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Adding an iOS view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Adding a macOS view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

Chapter 25: Building a Complete RxSwift App . . . . 413


Introducing QuickTodo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Architecting the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Bindable view controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Task model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Tasks service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Coordinating scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Binding the tasks list with RxDataSources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Binding the Task cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Editing tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432

raywenderlich.com 14
RxSwift - Reactive Programming with Swift

Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

raywenderlich.com 15
I Introduction

"If you've ever used an asynchronous callback based API, you've probably dealt with
handling the response data ad-hoc all across your codebase, and have most likely
decided there was no way to unit test it all... But, let me tell you - there is a better way,
and it's called Rx!"
— Krunoslav Zaher, creator of RxSwift

There’s no denying it: Rx is one of the hottest topics in mobile app development these
days!

If you visit international conferences, or even local meetups, it might feel like everyone
is talking about observables, side effects, and (gulp) schedulers.

And no wonder — Rx is a multi-platform standard, so no matter if it's a web


development conference, local Android meetup, or a Swift workshop, you might end up
joining a multi-platform discussion on Rx.

The RxSwift library (part of the larger family of Rx ports across platforms and
languages) allows you to use your favorite Swift programming language in a completely
new way. The somewhat difficult-to-handle asynchronous code in Swift becomes much
easier and a lot saner to write with RxSwift.

To create responsive and robust applications, you have to handle a multitude of


concurrent tasks like playing audio, handling user interface input, making networking
calls, and more. Sometimes, passing data from one process to another or even just
observing that tasks happen in the correct sequence one after another asynchronously
might cause the developer a lot of trouble.

raywenderlich.com 16
RxSwift - Reactive Programming with Swift Introduction

In this book, you’ll learn how RxSwift solves the issues related to asynchronous
programming and master various reactive techniques, from observing simple data
sequences, to combining and transforming asynchronous value streams, to designing
the architecture and building production quality apps.

By the end of this book, you’ll have worked through the chapter content and you’ll have
hands-on experience solving the challenges at the end of the chapters — and you’ll be
well on your way to coming up with your own Rx patterns and solutions!

What you need


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

• A Mac running the latest point release of OS X High Sierra or later: You’ll need
this to be able to install the latest version of Xcode.

• Xcode 10 or later: Xcode is the main development tool for iOS. You can download
the latest version of Xcode for free on the Mac app store here: https://
itunes.apple.com/app/xcode/id497799835?mt=12

• An intermediate level knowledge of Swift and iOS development. This book is about
learning RxSwift specifically; to understand the rest of the project code and how the
accompanying demo projects work you will need at least an intermediate
understanding of Swift and UIKit.

If you want to try things out on a physical iOS device, you’ll need a developer account
with Apple, which you can obtain for free. However, all the sample projects in this book
will work just fine in the iOS Simulator bundled with Xcode, so the paid developer
account is completely optional.

raywenderlich.com 17
RxSwift - Reactive Programming with Swift Introduction

Who this book is for


This book is for iOS developers who already feel comfortable with iOS and Swift, and
want to dive deep into development with RxSwift.

If you’re a complete beginner to iOS, we suggest you first read through the latest
edition of the iOS Apprentice. That will give you a solid foundation of building iOS apps
with Swift from the ground up but you might still need to learn more about
intermediate level iOS development before you can work through all chapters in this
book.

If you know the basics of iOS development but are new to Swift, we suggest you read
through Swift Apprentice first, which goes through the features of Swift using
playgrounds to teach the language.

You can find both of these books at our online store:

http://store.raywenderlich.com

How to use this book


Generally, each chapter in this book includes a starter project and covers a small
number of programming techniques in detail. Some of the chapters deal mostly with
theory so you get to try isolated pieces of code, while learning the process in a Swift
playground.

Other chapters provide you with a starter project that includes some non-Rx logic
inside and lead you through Rx-ifying the project by adding code in key places. In the
process, you’ll see what difference RxSwift makes in the project code and how to
approach different common problems.

We do suggest that you work through the chapters in order, since the concepts build
upon each other. Remember you’ll get the most out of the book if you follow along with
the tutorials and perform the hands-on challenges.

For advanced developers, there’s still value in the early chapters since they cover the
basics. However if you’re comfortable with those concepts, feel free to jump ahead to
the topics that interest you the most.

raywenderlich.com 18
RxSwift - Reactive Programming with Swift Introduction

What’s in store
This book is divided into six sections. You can find more details on each section in its
introduction. Here’s a brief overview.

Section I: Getting Started with RxSwift


The first section of the book covers RxSwift basics. Don’t skip this section, as you will be
required to have a good understanding of how and why things work in the following
sections.

Section II: Operators and Best Practices


In this section, once you've mastered the basics, you will move on to building more
complex Rx code by using operators. Operators allow you to chain and compose little
pieces of functionality to build up complex logic.

Section III: iOS Apps with RxCocoa


Once you've mastered RxSwift's basics and know how to use operators, you will move on
to iOS specific APIs, which will allow you to use and integrate your RxSwift code with
the existing iOS classes and UI controls.

Section IV: Intermediate RxSwift/RxCocoa


In this section, you will look into more topics like building an error-handling strategy
for your app, handling your networking needs the reactive way, writing Rx tests, and
more.

Section V: RxSwift Community Cookbook


Many of the available RxSwift-based libraries are created and maintained by the
community – people just like you. In this section, we'll look into a few of these projects
and how you can use them in your own apps.

Section VI: Putting it All Together


This part of the book deals with app architecture and strategies for building
production-quality, full-blown iOS applications. You will learn how to structure your
project and explore a couple of different approaches to designing your data streams and
the project navigation.

raywenderlich.com 19
RxSwift - Reactive Programming with Swift Introduction

Book source code and forums


This book comes with complete source code for each of the chapters — it’s shipped with
the PDF. Some of the chapters also include starter projects or other required resources,
and you’ll definitely want to have these on hand as you go through the book.

We’ve also set up an official forum for the book at forums.raywenderlich.com. This is a
great place to ask questions about the book, discuss debugging strategies or to submit
any errors you may find.

Book updates
Great news: since you purchased the PDF version of this book, you’ll receive free
updates of the book’s content!

The best way to receive update notifications is to sign up for our weekly newsletter. This
includes a list of the tutorials published on raywenderlich.com in the past week,
important news items such as book updates or new books, and a few of our favorite
developer links. Sign up here:

• www.raywenderlich.com/newsletter

License
By purchasing RxSwift: Reactive Programming in Swift, you have the following license:

• You are allowed to use and/or modify the source code in RxSwift: Reactive
Programming in Swift in as many apps as you want, with no attribution required.

• You are allowed to use and/or modify all art, images and designs that are included in
RxSwift: Reactive Programming in Swift in as many apps as you want, but must include
this attribution line somewhere inside your app: “Artwork/images/designs: from
RxSwift: Reactive Programming in Swift book, available at http://
www.raywenderlich.com.”

• This book is for your personal use only. You are NOT allowed to sell this book in
whole or in part without prior authorization, or distribute it to friends, co-workers or
students; they would need to purchase their own copy.

raywenderlich.com 20
RxSwift - Reactive Programming with Swift Introduction

All materials provided with this book are provided on an “as is” basis, without warranty
of any kind, express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and non-infringement. In no event
shall the authors or copyright holders be liable for any claim, damages or other liability,
whether in an action of contract, tort or otherwise, arising from, out of or in connection
with the software or the use or other dealings in the software.

All trademarks and registered trademarks appearing in this book are the property of
their respective owners.

About the cover


The electric eel is a unique kind of beast. In fact, it's been reclassified few times, since
it's not exactly like any other animal. It can grow to two meters in length and twenty
kilograms in weight.

Its size however, is not what should worry you, should you have the chance to meet one
in person. That little devil sports a natural taser gun that can discharge up to 860 volts
and 1 ampere of current! (Yeah, I hear you. Where was that electric eel when you were
trying to jump-start your car that morning last February, right?)

The electric eel was chosen for the Rx project logo since it's pre-release code name was
Volta. Now you know!

raywenderlich.com 21
Section I: Getting Started with
RxSwift

In this part of the book, you’re going to learn about the basics of RxSwift. You are going
to have a look at what kinds of asynchronous programming problems RxSwift
addresses, and what kind of solutions it offers.

Further, you will learn about the few basic classes that allow you to create and observe
event sequences, which are the foundation of the Rx framework.

You are going to start slow by learning about the basics and a little bit of theory. Please
don't skip these chapters! This will allow you to make good progress in the following
sections when things get more complex.

Chapter 1: Hello RxSwift!

Chapter 2: Observables

Chapter 3: Subjects

Chapter 4: Observables & Subjects in Practice

raywenderlich.com 22
1 Chapter 1: Hello, RxSwift!
By Marin Todorov

This book aims to introduce you, the reader, to the RxSwift library and to writing
reactive iOS apps with Swift.

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

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


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

Sounds 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.

raywenderlich.com 23
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!

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

You’ll start with the basic features of RxSwift, and then gradually work through
intermediate and advanced topics. Taking the time to exercise new concepts extensively
as you progress will make it easier to master RxSwift by the end of the book. Rx is too
broad of a topic to cover completely in a single book; instead, we aim to give you a solid
understanding of the library so that you can continue developing Rx skills on your own.

We still haven’t quite established what RxSwift is though, have we? Let’s start with a
simple, understandable definition and progress to a better, more expressive one as we
waltz through the topic of reactive programming later in this chapter.

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


code to react to new data and process it in a sequential, isolated manner.

As an iOS app developer, this should be much more clear and tell you more about what
RxSwift is, compared to the first definition you read earlier in this chapter.

Even if you’re still fuzzy on the details, it should be clear that RxSwift helps you write
asynchronous code. And you know that developing good, deterministic, asynchronous
code is hard, so any help is quite welcome!

Introduction to asynchronous
programming
If you tried to explain asynchronous programming in a simple, down to earth language,
you might come up with something along the lines of the following.

An iOS app, at any moment, might be doing any of the following things and more:

• Reacting to button taps

• Animating the keyboard as a text field loses focus

• Downloading a large photo from the Internet

• Saving bits of data to disk

• Playing audio

raywenderlich.com 24
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!

All of these things seemingly happen at the same time. Whenever the keyboard
animates out of the screen, the audio in your app doesn’t pause until the animation has
finished, right?

All the different bits of your program don’t block each other’s execution. iOS offers you
various kinds of APIs that allow you to perform different pieces of work on different
threads and perform them across the different cores of the device’s CPU.

Writing code that truly runs in parallel, however, is rather complex, especially when
different bits of code need to work with the same pieces of data. It’s hard to argue about
which piece of code updates the data first, or which code read the latest value.

Cocoa and UIKit asynchronous APIs


Apple provides lots of APIs in the iOS SDK that help you write asynchronous code.
You’ve used these in your projects and probably haven’t given them a second thought
because they are so fundamental to writing mobile apps.

You’ve probably used most of the following:

• NotificationCenter: To execute a piece of code any time an event of interest


happens, such as the user changing the orientation of the device or the software
keyboard showing or hiding on the screen.

• The delegate pattern: Lets you define an object that acts on behalf, or in
coordination with, another object. For example, in your app delegate, you define
what should happen when a new remote notification arrives, but you have no idea
when this piece of code will be executed or how many times it will execute.

raywenderlich.com 25
RxSwift - Reactive Programming with Swift Chapter 1: Hello, RxSwift!

• Grand Central Dispatch: To help you abstract the execution of pieces of work. You
can schedule code to be executed sequentially in a serial queue, or run a multitude of
tasks concurrently on different queues with different priorities.

• Closures: To create detached pieces of code that you can pass around in your code,
so other objects can decide whether to execute it or not, how many times, and in
what context.

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

After all, your app’s code runs differently depending on various external factors, such as
user input, network activity, or other OS events. Each time the user fires up your app,
the code may run in a completely different order depending on those external factors.
(Well, except for the case when you have an army of robots testing your app, then you
can expect all events to happen with precise, kill-bot synchronization.)

We’re definitely not saying that writing good asynchronous code is impossible. After all,
the great APIs from Apple listed above are very advanced, very specialized for the task
and, to be fair, quite powerful compared to what other platforms offer.

The issue is that complex asynchronous code becomes very difficult to write in part
because of the variety of APIs that Apple’s SDK offers:

Using delegates requires you to adopt one particular pattern, another one for closures,
yet another approach for subscribing to NotificationCenter, and so on. Since there is
no universal language across all the asynchronous APIs, reading and understanding the
code, and reasoning about its execution, becomes difficult.

To wrap up this section and put the discussion into a bit more context, you’ll compare
two pieces of code: one synchronous and one asynchronous.

raywenderlich.com 26
Exploring the Variety of Random
Documents with Different Content
bank and many of the drooping branches of the trees are
submerged; the natives approach quietly in their canoes, cautiously
push the net under the submerged vegetation, and then with a
sudden jerk lift it up out of the water, in this way capturing numbers
of small fish which had been sheltering or looking for food among
the leaves.
Another form of fishing net—though there is no netting in its
construction—is made of long, thin strips of bamboo tied parallel to
each other at intervals of about half an inch, forming a sort of screen
or trellis-work, which can be rolled up if necessary. Strong wooden
stakes are driven into the mud at the mouth of the creeks which join
the river in many places, and at high water the screens are fastened
to the stakes in such a way as to touch the bottom and close the
entrance of the creek; the water can run back when the tide falls,
but not the fish which are sometimes caught in considerable
numbers.
The larger fish are all obtained by the men, who either catch them
with a hook and line, or spear them in the shallow water near the
river mouth, or along the sea shore. We saw very few hooks; one or
two were made of rough metal, the others were neatly fashioned
from fish bones, and all of them were plain without barbs. Now they
have a large number of steel fishhooks, which they greatly value.
The commonest types of fish-spear are made of thin bamboo or a
light wood about ten feet long, and they end in three or four sharp
prongs of bamboo or hardened wood. They also use a barbed spear
of which the head becomes detached from the shaft, when it
becomes fixed in a fish; a light line connecting the shaft with the
head causes the shaft to act as a drag on the movements of the fish,
which can easily be followed up and killed; this kind of spear is only
used for the larger fish, saw-fish and the like, but I never saw it in
use. Considering the enormous number of fish that there are—at the
mouth of the river the water is sometimes seen to be seething with
large fish—it cannot be said that the men are very clever with their
spears.
They also shoot fish, using single- or three-pointed arrows; you may
see a man standing quietly in a pool of water like a heron waiting for
the fish to come up to him, or stalking a shoal of fish stealthily from
the bank; in either case he will probably shoot arrow after arrow
without effect, for they are absurdly indifferent marksmen with the
bow.
The most primitive methods of all of catching fish I saw practised
one day coming down from Obota. A native paddling in the bow of
my canoe saw a large fish near the bank, towards which he steered
the canoe. When he judged that he was near enough to it, he hurled
himself flat on to the water with a resounding splash that drenched
everything in the boat, and a thud that would have stunned the fish
at once had it not darted off an instant earlier.
The sight of a fish, however small it is, always rouses a Papuan to
action. When we were travelling with natives, we sometimes came
to pools where small fish had been left by some receding flood.
Instantly their loads were thrown down and everyone darted into the
water with sticks and stones and shouts and as much enthusiasm as
if the fish had been salmon and a full meal for everyone.
There is another method of fishing which was observed by the
navigator, Captain Dampier, in use by the natives of this region. It is
so remarkable that, although we did not see it employed by the
people of the Mimika district, I shall make no excuse for repeating it
here:—
“They strike Fish very ingeniously with Wooden Fiss-gigs and have a
very ingenious way of making the Fish rise: For they have a piece of
Wood curiously carv’d and painted much like a Dolphin (and perhaps
other Figures;) these they let down into the Water by a Line with a
small weight to sink it; when they think it low enough, they haul the
Line into their Boats very fast, and the Fish rise up after this Figure;
and they stand ready to strike them when they are near the Surface
of the Water.”11
There are times when the natives get more fish than they know
what to do with, and other times when no fish can be caught; but
they have no idea of laying up a store for the lean times. It is true
that they char some in the fire and keep them for a few days before
the fish putrify, but if they learnt to smoke some of their surplus
supply, they need never go hungry.
CHAPTER X
Food of the Papuans—Cassowaries—The Native Dog—Question of Cannibalism—
Village Headman—The Social System of the Papuans—The Family—Treatment
of Women—Religion—Weather Superstitions—Ceremony to avert a Flood—The
Pig—A Village Festival—Wailing at Deaths—Methods of Disposal of the Dead—
No Reverence for the Remains—Purchasing Skulls.

The search for food furnishes occasionally FOOD OF THE NATIVES


some very curious scenes. One of the most
remarkable occurs when the river in flood brings down a tree-trunk
in a suitable stage of decay. A canoe is sent out with men to secure
it and tow it to the bank. When it has been left stranded by the
falling water, the people, men, women and children come out and
swarm around it like bees about a honey-pot, and you wonder what
they can be doing. When you go close you find that some are
splitting up the log with their stone axes and others are cutting up
the fragments with sharpened shells in the same way that their
ancestors—and perhaps ours too—did centuries ago. The objects of
their search are the large white larvæ of a beetle, about the size of a
man’s thumb; I have seen natives eat them just as they cut them
out of the wood, but usually they roast them in the fire and consider
them a great delicacy.
Nothing that can by any means be considered eatable comes amiss
to the Papuans; there are two kinds of water tortoises which they
like to eat, and rats, lizards, frogs and snakes, and the eggs of
crocodiles they devour greedily. A number of different kinds of fruits,
most of them disagreeable to European tastes, are found growing in
the jungle and form a welcome addition to their fare. Birds they get
occasionally, but their skill with the bow and arrow is not
remarkable.
Most of their meat is obtained by hunting with dogs the wild pig, the
wallaby and the cassowary. The pig (Sus papuensis), though it is not
really a native of New Guinea, was introduced into the island so long
ago that it has become as well established as the rabbit has become
in this country. In some places, particularly near the foot of the
mountains, pigs are fairly numerous, and the natives kill a good
many; they are very savage beasts, and I saw a native terribly
gashed by a large boar, which was shortly afterwards shot by one of
our Gurkhas.
The Wallaby (Dorcopsis lorentzii) is a small kangaroo, about two feet
in height when it stands upright; it seems to be fairly evenly
distributed all over the district. When the natives bestir themselves
they seem to be able to catch the wallaby fairly easily; in four
consecutive days we saw the remains of thirteen brought into the
village of Parimau. The flesh is coarse and has a very strong musky
flavour.
There are two kinds of Cassowary in the Mimika district, a small
species new to science (Casuarius claudi), which was discovered in
the mountains at an altitude of about 1500 feet, and a large species
(Casuarius sclateri), which was fairly abundant everywhere. We
frequently heard their curious booming cry at night and we often
saw their tracks in the mud of the jungle or on the river bank, but
they are very shy birds and are seldom seen.
Once I had the luck to see an old cassowary with two young birds
walking about in a stony river bed, a place which they particularly
affect, and it was a very pretty sight to see how the mother bird,
after she had caught sight of me, drove away the chicks to a place
of safety and all the time kept herself between them and me. The
natives hunt and kill and eat a good many cassowaries; the feathers
are used for ornamental head-dresses and belts and for decorating
spears and clubs, and the claws are often used as the points of
arrows.
The Papuan Dog, without whose help the native THE NATIVE DOGS
would seldom, if ever, be able to get any meat,
is a sharp-nosed prick-eared creature about the size of a Welsh
terrier. The colour is yellow, brown or black, and the tail, which is
upstanding, is tipped with white. Usually the hair is short and
smooth, but we saw one dog, brought down to Parimau by a party
of pygmies, which had a thick furry coat like a chow dog, which it
also resembled in the carriage of its tail. The dogs in the village of
the pygmies which we visited, were smooth-coated like those of the
Papuans, so it is possible that that thick-coated animal came from
some remote district where the natives live at a higher altitude.
The Papuan dogs are very sociable creatures, and they like to
accompany the natives on their journeys. They are particularly fond
of going in canoes on the river, and two or three are seen in nearly
every canoe even when the people are only out fishing. Their food is
generally given to them by the women and it consists of raw meat,
when there is any, and lumps of sago. A remarkable peculiarity
about them is that they never bark, but they make up for this defect
by their extraordinary power of howling. Sometimes in broad
daylight, if there was no wind, but more often on still fine nights, a
party of dogs would sit together, usually on the river bank, and utter
a chorus of the most piteous and blood-curdling howls. No amount
of stone-throwing or beating with sticks, freely administered by their
masters, had the smallest effect on them; they would only move
away a few yards and begin again, apparently carried away by an
ecstasy of sorrow.
The natives value their dogs highly, as they well may do, for they
provide the whole of their meat supply, and they use them to
exchange for articles of which they have great need. The people at
Parimau have a small piece of iron about the size of a chisel, used
for carving their canoes and paddles, for which the enormous price
of three dogs had been paid, so they informed us, to the people of
the Wakatimi. One day one of our “boys” shot a dog, which had
been in the habit of stealing food from our camp. When the natives
knew that it was dead, all the people of the village began to wail in
the same manner as they do when a person dies, and the owner of
the dog smeared himself with mud and mourned bitterly. No doubt
the display was somewhat exaggerated in the hope of getting a
compensation from us, but at the back of it there was genuine
emotion.
Before leaving the subject of the food of the Papuans and their
means of obtaining it, a word must be said on the question of
cannibalism. It is popularly supposed that all the natives of New
Guinea are cannibals, and fears were expressed by many of our
friends that some, if not all, of us would end in a Papuan feast. But
we saw no signs of cannibalism, and we have no reason to suppose
that it is practised by the people of the Mimika district. Men whom
we questioned about it denied it and showed expressions of disgust
at the suggestion; but that is not a complete proof of their
innocence, for I have known people elsewhere, who were
undoubtedly cannibals, deny it in the same manner. The question of
cannibalism is always difficult to decide without direct evidence, and
in the case of these Papuans the verdict must be one of “Not
proven.”
The account given in a preceding SOCIAL SYSTEM OF THE PAPUANS
chapter of the difficulties we
experienced in learning the language of the Papuans will serve to
explain how it was that we learnt so little about the nature of their
social system. The people of Wakatimi were called Wakatimi-wé
(people of Wakatimi), the people of Obota were Obota-wé, and the
people of other villages in like manner, but we never heard one word
that included them all, nor indeed do we know whether or not they
consider themselves all to belong to the same tribe.
In every village that we visited there were one or two or even more
men who called themselves natoo, a word signifying “chief.” But in
no case did the natoo appear to have any authority over the other
people; their houses were no bigger than the rest, and (except in
one instance) they had no more personal property than the other
members of the community.
A PAPUAN OF MIMIKA.
The exceptional case was a man of unusual intelligence who became
our intimate friend and gave us much information for which he was
always well rewarded, so that before we left the country his house
was filled with tins and bottles, and he was the possessor of axes
and knives, yards of cloth and countless beads. In all the ordinary
affairs of life the “chiefs” and their families have to work like
everybody else, but it is possible that in their wars, of which we saw
nothing at all, they may be persons of more consequence.
Generally speaking, one would say that the society of the Mimika
Papuans is a group of small families. It cannot by any means be
described as a socialistic community; with one exception there is no
sign of community of property, but it is rather a case of every man
for himself, or (more accurately) of every family for itself. A canoe
belongs to the family of the man who made it; the coconut trees,
which grow here and there along the lower Mimika, do not belong to
the community but to individuals, presumably the men or some of
the men who planted them. Sometimes the trees are protected by a
fence, a very flimsy structure of three or four sticks, placed across
the track which leads to the trees; in other cases a few palm leaves
or some pierced shells threaded on a string are tied round the tree
itself; both of these devices appear to be enough to ensure the
security of the trees. The exception mentioned is seen when game is
brought in by the hunters; the meat, as I observed on several
occasions, is distributed to every house in the village.
As I have described above (p. 97) the houses in a village are joined
together under a common roof, but each family enters by its own
doorway, and, except for the publicity resulting from the lack of
dividing walls or partitions, it finds itself in its own private house. It
is difficult to say exactly of what the “family-group” consists. There
are the man and his wife and the children, and sometimes an extra
man or two, and, rarely, an extra woman, who is, I believe, always a
second wife of the man of the house; but the position of the extra
men and their relationship to the rest of the family I cannot define.
At the village of Obota a detached house, rather larger than the rest,
was said to be occupied by young men only; we did not see any
other instance of this elsewhere.
Families are small, as might be expected from the severity of their
conditions of life and the long period of suckling by the mothers, and
we did not know definitely of any couple who had more than three
living children. Though the women do a large amount of the work of
the community they are not mere drudges; they do a great deal of
talking, and the men appear to pay considerable respect to their
opinion. This was frequently noticeable when we wanted to buy
something, such as canoes, from a native; he would say that he
must first of all go and consult his wife, and when he returned it
often happened that, prompted by his wife, he insisted on a higher
payment than he had asked before.
On one occasion only did we see a woman ill-treated, and the
performance was a particularly brutal one. Two men and a woman
walked down from the village of Wakatimi to the river bank,
dragging another woman, who shrieked and struggled violently.
After throwing her into the mud they dragged her into the shallow
water and tried to drown her by holding her down under a fishing-
net. We shouted at them, and were just going with some soldiers in
a canoe across the river to rescue the woman, when they desisted
and allowed the poor creature to crawl out on to the bank, where
she lay for some time exhausted. Some natives who came over to us
shortly afterwards laughed about it and treated the whole affair as a
joke.
With regard to the superstitions and beliefs PAPUAN SUPERSTITIONS
of the Papuans, owing to our unfortunate
difficulties with the language we learnt nothing whatever. Religion, in
the accepted sense of that term, I am sure they have not. It is true
that they make curious carved effigies, but these are not idols, and
there is no evidence to show that they ever consult or worship them;
on the contrary, they treat them with contempt and often point to
them with laughter. These images are ingeniously and skilfully
carved out of wood, and they represent a human figure always
grotesque and sometimes grossly indecent. They vary in size from a
few inches to twelve or fourteen feet, and when they are not
neglected they are ornamented with red and white paint.
We had opportunities of observing the outward signs of what were
probably superstitions in connection with certain phenomena of the
weather. For instance, the first peal of thunder that was heard in the
day—it occurred almost every day—was greeted by the men with a
long-drawn tremulous shout. On the occasion of a particularly
alarming thunderstorm, when the lightning flashes were almost
unceasing, the men came out of doors and with long sticks beat the
ground in front of their huts; then they waved the sticks in the air,
shouting loudly meanwhile. Curiously enough the rare whistle of a
certain bird, which we never identified, was always greeted by the
men of Parimau with a shout precisely similar to that with which
they greet the thunder.
The first sight of the new moon was signalised by a short sharp bark
rather than a shout. Several times on the day following the first sight
of the new moon I noticed a spear decorated with white feathers
exposed conspicuously in the village, but whether it had any
connection with the kalendar I cannot say.
When the first drops of rain of the day began to fall, the men were
sometimes seen to snap their fingers four times towards the four
quarters of the compass.
A curious ceremony was twice observed at a time of heavy rain,
when the Mimika was rising rapidly and threatening to sweep away
the village of Parimau. A party of men walked down to the edge of
the river, and one of them with a long spear threshed the water,
while the others at each stroke shouted, “Mbu” (water, flood). Then
they went up to the village, and in front of each door they dug a
hole, into which they poured a coconut-full of water; again they
shouted “Mbu,” and then filled up the hole with sand.
That they have some belief in the supernatural is certain. We learnt
a word niniki, which undoubtedly means ghosts; they described
niniki as things which you could not see but were here and there in
the air about you. When they were asked where a dead man had
gone to, they talked of niniki, and pointed vaguely to the horizon,
saying the word which means “far.”
If there is one thing in heaven or earth to which it may be PIGS
said that the Papuans pay some sort of respect it is the pig.
They hunt and kill a good many wild pigs in the jungle and eat their
flesh, but the lower jaw of each animal is carefully cleaned and hung
up on a sort of rack in front of the houses; on one of these racks I
counted no fewer than thirty-two pigs’ jaws. The grass and leaves in
which the animal is wrapped and the ropes used for tying it up when
it is carried home from the jungle, are not thrown away but are
hung up on a similar sort of rack in a conspicuous place in the
village.
In every village there may generally be seen two or three pigs
running about freely; they are probably not bred in the village, but
are caught in the jungle, when they are young. They very soon
become quite tame and accompany the people on their migrations
from one place to another until they are full grown, when they
provide food for a festival. The only elaborate popular ceremony that
took place while we were in the country happened early in May at
Parimau, and the principal feature of it was the slaughter of pigs.
Unfortunately for me I was at the base-camp at the time and did not
see the festival, so I will make extracts from Marshall’s graphic
account.12
“Yesterday the natives gave us an excellent A VILLAGE FESTIVAL
show. For some days previously natives had
been arriving from distant parts until the small village of 40 huts
contained 400 people, and it was evident from the tomtomming and
other signs that something of importance was about to take place.
On the night of the 3rd inst. they lit a big bonfire, and all night long
they were howling and yelling as if to drive away evil spirits. Soon
after daybreak they came over to fetch us, and, expecting something
unusual, I slipped a film into my cinematograph camera and went
over. They gave me every opportunity of obtaining a good picture,
keeping an open space for me in the best positions. First of all the
women, draped in leaves, slowly walked down the beach, driving
two full-grown boars in front of them, and then disappeared in the
jungle. About 150 men with faces painted and heads and spears
decorated with feathers, formed up in three sides of a square, one
end of which was occupied by a band of tomtoms. A slow advance
on the village then commenced, the men shouting in chorus and the
women dancing on the outskirts. The centre of the square was
occupied by single individuals, who, following each other in quick
succession, gave a warlike display, finally shooting arrows far over
the trees.

A TYPICAL PAPUAN OF MIMIKA.


“The next scene took place around a large sloping erection which we
soon found was an altar, on which the two boars were about to be
sacrificed. The women and boars who had disappeared into the
forest now marched from the jungle at the far end of the village.
The boars were seized, and a struggle with the animals ensued, but
the two huge brutes were bound up with rattan, chalk meanwhile
being rubbed into their eyes, apparently in order to blind them. The
women set up a tremendous wailing, and appeared on the scene
plastered in wet mud from head to foot. The two boars, on each of
which a man sat astride, were now hoisted up and carried to the
altar, on which the animals were tightly lashed. Then amid much
shouting, tomtomming, and fanatical displays, the boars were
clubbed to death. As soon as life was extinct, the women cut the
carcases free, and, pulling them to the ground, threw themselves on
the dead bodies, wailing loudly, and plastering themselves with wet
mud in ecstasies of grief. This continued for some ten minutes, when
the men, many of whom were covered with mud and uttered
strange dirges, picked up the bodies, and the whole assembly
following suit marched into the river, where a much-needed washing
took place. Just previous to this a three-year-old child, painted red
and crying loudly, had been roughly seized and dragged towards the
dais, and for a moment we thought something more serious than a
boar sacrifice was about to take place. But we were much relieved to
see that it was only having its ears pierced. The whole performance
lasted about an hour and a half.
“The afternoon was given over to innocent play, the women and girls
—many of them quite pretty—chasing the men up to the river side
and into the water. This is one of the few ceremonies when the
women are allowed to beat the men, the latter not being permitted
to retaliate. The damsels finally became so bold that they stormed
the camp.”
Of ceremonies connected with birth, if any take place, we saw
nothing at all. The only marriage ceremony that took place during
our stay in the country has been referred to on a preceding page.
Deaths were unfortunately more frequent, and if they were not
accompanied by any elaborate ceremonial they were, at all events,
widely advertised, sometimes indeed even before the event itself. A
wretched man became very ill at Parimau in August, and it was soon
evident that his days were numbered. Members of his family carried
him out of the house and laid him in the sunlight for a time, and
then took him back into the house again at least half a dozen times
a day. Now and again, when he dozed, they set up the dreadful wail
that is customary when a person dies, and he had to wake up and
assure them of his continued life. At night his hut was crowded with
sympathetic watchers, and with the smoke of the fire and much
tobacco the atmosphere must have been nearly insupportable. As
our own house was distant only about forty yards across the river we
could plainly hear his laboured breathing, and when it grew softer
they wailed again until the wonder was that he did not die. On the
third day they dug a grave for him, but still he lingered on, and it
was not until the fifth night, when a tremendous flood came down
and swept away the village so that all the people had to take refuge
in their canoes, that he died.
When a death occurs the people in the hut at WAILING AT DEATH
once begin to wail, then the people in the
neighbouring huts join in and soon the whole village is wailing. It is
a very peculiar and very striking chorus. Each individual wails on one
note, and as there are perhaps five notes ranging from a very high
pitch to a deep murmured bass being sung at once, the effect is
most mournful. The occasional beat of a drum adds not a little to the
general effect of lamentation. It must be admitted, however, that the
wailing is not always a musical performance. Sometimes the
mourning man behaves in the way that a child does when it is
described as “roaring”; he puckers up his face in the most
extraordinary contortions, “roars” at the top of his voice with
occasional heart-breaking sobs, while the tears course down his
face, and the complete picture is ludicrous in the extreme.
The disposal of the dead nearly always takes place just before dawn,
but the method of it is not always the same. The most common
practice is to bury the body in a shallow grave dug in the nearest
convenient spot, sometimes within a few yards of the huts. The body
is wrapped in mats and laid flat in the grave, which is then filled up,
and its place is perhaps marked by a stick, but in a day or two it is
forgotten and people trample on it without heed.
We observed one instance of a more elaborate kind of burial. The
corpse, wrapped in leaves and mats, was taken out into the jungle
and placed on a platform about four feet high, which had been put
up for the purpose. After placing the body on the platform the men
who had carried it walked down to the river, shouted once in unison,
and then, having received an answering shout from the men in the
village, one of them threw a small triangular piece of wood out into
the stream. In the meantime the family of the dead man
disappeared into the jungle, from which they soon emerged quite
naked, plastered all over with mud and decorated with wisps of
climbing plants. The next two days were spent in digging a grave
and making a coffin shaped like a small canoe; this however was
found to be too small and was not used. On the third day the body
was placed in the grave, and an ornamental post placed in the
ground at each end, but contrary to our hopes (for the state of that
man was becoming very offensive) they did not fill in the grave.
They merely covered the body with leaves and turned it over every
day. At intervals the widow, quite naked, save for a plastering of
mud, crawled on hands and knees from her hut, which was less than
five yards distant, and visited the grave. In a few days a providential
flood came and filled up the grave and put an end to what had
become for us an almost intolerable nuisance.
Both at Wakatimi and at Parimau our camp commanded a good view
of the native village, and a death always provided us with the mild
excitement of wondering in what new way they would celebrate the
event. On one occasion when a woman died, the bereaved husband
and another man walked slowly down to the river and waded out
into about three feet of water. There the widower submitted to being
washed all over by the other man and finally to being held under
water by him for half a minute or more, after which they walked
solemnly back to the village.
Early in the morning of the day after the DISPOSAL OF THE DEAD
death of the natoo of Wakatimi all the
women and girls of the village, to the number of sixty or seventy,
came down to the river, all of them without a vestige of clothing,
and in the shallow water a foot or two deep they swam and crawled
and wriggled up the river for a hundred yards or more, wailing loudly
all the time. Sometimes they came out on to the bank and rolled in
the mud, and finally they all went out of the water and stood wailing
in front of the dead man’s house.
Another method of disposing of the dead, which is very frequently
adopted, is to place the body wrapped in mats in a rude coffin,
which is usually constructed from pieces of broken canoes. The
coffin containing the body is supported on a trestle of crossed sticks
about four feet from the ground (see illustration opposite), and there
it remains until decomposition is complete. As these coffins are often
placed within a yard or two of the houses, it can be imagined that a
Papuan village is not always a pleasant place to visit.
At the village of Nimé we saw two or three pathetic little bundles
containing the remains of infants exposed on racks within a few feet
of the houses, from which they doubtless came.
DISPOSAL OF THE DEAD. A COFFIN ON TRESTLES.
When decomposition is complete no account is taken of the bones,
excepting the skull, which is taken and preserved in the house.
Sometimes it is buried in the sand of the floor of the house, and
sometimes it is tied up in a sort of open basket-work of rattan and
hung up in the roof, where it becomes brown with smoke and
polished by frequent handling.
Though the people take the trouble to bring the skulls into their
houses, they show no real respect for them, and they are eager
enough to part with them if a chance occurs. Two of us went one
day to Obota, a village a few miles from Wakatimi, in the hopes of
buying some bananas. In one of the huts we saw a skull and offered
to buy it, not at all expecting that the owner would be willing to sell,
but the offer of (I think) a piece of cloth was gladly accepted and
the skull was ours. In a few minutes, when it became known that we
had given good cloth for a common skull, everybody was anxious to
sell his family remains, and outside every doorway were placed one
or two or even three grinning skulls. They do not treat the skulls
very carefully, and a good many were damaged, so we only bought
about half a dozen that were perfect.
One day a man walked into our camp at Wakatimi carrying a skull
under his arm. He stood outside our house for some time, grinning
and saying nothing, then he gave us unmistakably to understand
that it was the skull of his wife, who, as we knew for a fact, had only
died a short time previously. The skull was indeed so fresh that we
declined the offer.
CHAPTER XI
Papuans’ Love of Music—Their Concerts—A Dancing House—Carving—Papuans as
Artists—Cat’s Cradle—Village Squabbles—The Part of the Women—Wooden
and Stone Clubs—Shell Knives and Stone Axes—Bows and Arrows—Papuan
Marksmen—Spears—A most Primitive People—Disease—Prospects of their
Civilisation.

The most pleasing characteristic of the Papuans is their love of


music. When a number of them are gathered together and when
they have eaten well, or are for any other reason happy, they have a
concert. Sometimes the concerts take place in the afternoon and
continue till nightfall, but more often they begin after dark and go on
almost through the night. The orchestra is simple and consists of
two or three men who beat drums and sit before a small fire in the
middle. Round them are grouped the chorus all sitting on the
ground. The drums are hollowed cylinders of wood, which are often
elaborately carved; one end is open, the other is closed by a piece of
lizard’s or snake’s skin (see illustration p. 142). When this skin
becomes slack, as it very quickly does, the drummer holds it towards
the fire until it regains its pitch. It is not the custom to tune up both
drums, when there are more than one, to the same pitch, usually an
interval of about half a tone is left between them. The leader of the
orchestra sometimes wears a remarkable head-dress made of plaited
fibre and ornamented with bunches of plumes of the Bird of Paradise
(see illustration p. 78). The effect of these plumes waving backwards
and forwards as the man moves his head to mark the phrases of the
song is exceedingly striking, and it must be admitted that if there is
anybody, who can becomingly wear those gorgeous plumes, it is the
naked black man.
The most usual kind of song begins with a slow tapping of the
drums, then these are beaten quicker and the singer (one of the
drummers) begins a sort of recitative song, to which the chorus
contributes a low humming accompaniment. Then the drums are
beaten very loudly and rapidly, all the men in chorus sing, or rather
growl, a deep guttural note, followed by a prolonged musical note at
about the middle of the register of a normal man’s voice, and the
song ends with one or more short sharp barks, “Wah! wah! wah!”
with a loud drum accompaniment. The song, or probably different
verses of it, is repeated very many times. The final shouts of the
song, which for want of a better word I have called “barks,” are
uttered by all the men in unison and recall, as was pointed out by
Mr. Goodfellow, the harsh croaking call of the Greater Bird of
Paradise, which is heard almost daily in the jungle. It is possible that
the song is in some way connected with the bird and that there is an
intentional imitation of its note.
The scheme of all these songs is the same, viz., a recitative with
drums and a humming accompaniment, but some of them have
really rollicking choruses, and we used to listen to them at night with
extreme pleasure as they came, somewhat softened by distance,
over the water to our camp at Wakatimi. The voices of the men are
often rich, and they have a true musical ear. Their intervals are very
similar to ours and not at all like those of the Malays and many other
Eastern singers, who recognize perhaps five notes where we have
only two. Beside the drum the only instrument of music they have is
a straight trumpet made from a short piece of bamboo. This
produces only a single booming note and is not used at the concerts.
1. Stone Axe.
2. 3. 4. Head-rests for sleeping.
5. 6. 7. Drums.
As an amusement of the Papuans even more A DANCING HOUSE
important than singing is dancing, of which they
often talked, but though we saw some of their dancing halls (see
illustration p. 48), we never had the good fortune to witness a
performance. At the coast village of Nimé, a few miles to the East of
the Mimika River, there was a very elaborate dancing house, which
must have cost an immense amount of labour to build. The length of
the house from front to back was about 100 feet, the width about 25
feet, and it rested on poles which were about 8 feet high in front,
rising up to about 14 feet high at the back. The side walls and the
back were of “atap” as was also the roof, which sloped from a long
ridgepole running the whole length of the house. The ridgepole was
remarkable as being made from a single tree trunk (Casuarina)
shaved down very smoothly to a uniform thickness of about 10
inches; the ends of it, which projected about 8 feet both at the front
and back of the house, were carved in very lifelike representations of
the head of a crocodile and were painted red. The weight of the
beam must have been immense and one wondered how it had been
hoisted into position. Between the ridge of the roof and the eaves
there projected both in front and at the back six other smaller poles
grotesquely carved to represent fish and reptiles and hideous human
heads. The front of the house was open, and when you had climbed
up the supporting poles and had stepped over a low fence you found
yourself in a spacious hall with a floor well made of sheets of bark,
which sloped up gradually from front to back. Along either side at
regular intervals on the floor were sand fireplaces and above these
were wooden racks, from which it was evident that something was
hung to be cooked. Round the walls on all sides was a strip of
carved and painted wood, and exactly in the middle of the hall, fixed
to the floor and the roof were two posts about 3 feet apart and tied
between them, at about half the height of a man, was an elaborately
carved and painted board about twelve inches wide. In the middle of
this board was carved the eye, which is a familiar feature of the
ornamental carving on the canoes and drums, and it appeared that
this eye is the centre of the ceremonies which take place in the
house.
So far as I could understand from the description of the natives who
accompanied me in my visit to the house, the people, both men and
women, who take part in the ceremony, dance slowly upwards from
the front of the house singing as they go, and when they reach the
carved board each one in turn touches the eye, while all the people
shout together. But what the object of the whole performance is and
what the people cook and eat, are questions to which I was unable
to find an answer.

1-7. Bamboo Penis-cases. 8-12. Carved Blades of Paddles.


I have had occasion above to mention the artistic PAPUAN ARTISTS
carvings on the canoes and drums. Their paddles
too show a very good idea of design, as will be seen from the
illustration p. 144. Nothing amused them more than to be provided
with a pencil and pieces of paper and to attempt to draw figures.
Their efforts were not always very successful, and some of the
drawings which I have kept would be quite unrecognisable for what
they are, if I had not labelled them at the time. Like the young of
civilised races they always preferred to draw the figures of men and
women, and some of these are remarkable for having the mouth
near the top of the head above the level of the eyes. The method of
drawing is very simple; the pencil is held almost upright on the
paper and the outline of the figure, begun at an arm or leg or
anywhere indifferently, is drawn in one continuous stroke without
removing the pencil from the paper. The end is always rather
exciting, like the feat of drawing a pig when you are blindfolded, for
the artist is never quite certain of finishing at the point whence he
started. Besides human figures they liked drawing dogs, pigs, birds
and fishes. Two pictures of a dog and a bird both done by the same
man are peculiarly interesting, because they were both drawn upside
down. I watched the man making the drawings, and when they were
finished I saw that the legs of the creatures were uppermost; so I
turned the papers the right way round and handed them back to
him, but he inverted them again and admired them in that position.
Curiously enough the same man drew human figures in the correct
attitude, head uppermost, so that the state of his mental vision
offers rather a puzzling problem.
a. Cockatoo. a1 b. Designs for scarification. b.
Hornbill.
c. Pig. d. Dog. e. Bird. f. Man. g. Woman.
Most of them had a keen appreciation of pictures and they were
surprisingly quick in identifying photographs of themselves; in this
respect they showed a good deal more intelligence than some of our
Gurkhas, who held a photograph sideways or upside down and
gazed at it blankly, as if they had not the faintest idea of what it
portrayed. The illustrated papers were a source of endless delight to
them, and the portraits of beautiful ladies, who they felt sure were
our wives, were greatly admired. Horses, sheep, cattle and all other
animals were declared to be dogs.
Another amusement—it can hardly be called an art CAT’S CRADLE
—of the Papuans is the game of cat’s cradle, at
which many of them are extraordinarily proficient. It is not, as with
us, a game played by two persons; with them the part of the second
person is performed by the player’s teeth, and he contrives to
produce some wonderfully intricate figures, none of which, I regret
to say, we had patience or skill enough to learn. The most elaborate
figure I saw was supposed to represent a bird, and when the
features of it had been pointed out some resemblance was certainly
apparent.
But it must be admitted that their amusements are not always so
innocent as drawing pictures and playing cat’s cradle. I have referred
above to the gang of drunkards, who used to create such turmoil at
Wakatimi. The people of Parimau, who had no means of getting
intoxicated, were just as quarrelsome as the Wakatimi people, and
fights were of frequent, almost daily occurrence. Some one does
something, it matters not what, to offend some other person, and in
an instant the village is in an uproar. Spears fly through the air—we
never saw anybody touched by one—and stone clubs are brandished
furiously, the combatants all shout horrible threats at the tops of
their voices, while a few people look on stolidly or hardly take any
notice at all. There seems to be a certain etiquette about the use of
clubs, for the person about to be hit generally presents a soft part of
his person, the back or shoulders, to the clubber, and we never saw
a man intentionally hit another on the head, a blow which might
easily be fatal; but blood flowed in plenty from the flesh wounds.
The part of the women in these village squabbles is always to
scream loudly and generally to begin by banging the houses with
sticks or spears and to end with pulling them to pieces. In a fight at
Wakatimi we saw a party of infuriated women absolutely demolish
three or four houses. The fights end almost as suddenly as they
begin and in a short time the village settles down to its usual
tranquillity. Neither the sight nor the sound of these village quarrels
is very agreeable, but they have no regularly organised games and,
at the worst, not a very great amount of damage is done.
The clubs used in these village fights and doubtless also in their
tribal wars—but of those we know nothing—are of two kinds,
wooden and stone-headed. The wooden clubs are about four feet
long and consist of a plain shaft, of which the last foot or rather
more is carved into a saw-like cutting edge; some of these are made
of a very heavy wood and they are exceedingly formidable weapons.
A more simple type of wooden club is a plain wooden shaft rather
thinner at the handle end than at the other, round which is fixed a
piece of shark’s skin or the prickly skin from the back of the Sting
Ray and often with it is tied the saw of a small Saw fish; such a club
appears to be capable of inflicting a very nasty wound.
SPLITTING WOOD WITH A STONE AXE.
There is a great variety of stone-headed clubs, but STONE CLUBS
they are all alike in being furnished with a wooden
shaft, which is usually a plain piece of wood, but occasionally carved
near the club end. The stone head is pierced in the middle by a
round hole about an inch in diameter, through which the shaft is
passed and fixed firmly by wedges. Most of the heads are made of a
rather soft limestone, but where the people obtain it we do not
know, for there is no stone of any kind near the coast. The simplest
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