SlideShare a Scribd company logo
1 of 54
Download to read offline
RXSWIFT
REACTIVE PROGRAMMING WITH
WHO AM I?
▸ iOS developer since 2010
▸ Swift since day 1
▸ Transitioning to Swift
▸ CocoaConf
▸ Wash U
▸ RayWenderlich.com
▸ lynda.com
WHO ARE YOU?
▸ iOS developer
▸ How long?
▸ Swift developer
▸ How long?
▸ Reactive programming
▸ ReactiveCocoa
▸ Reactive Extensions
WHAT IS REACTIVE PROGRAMMING?
REACTVE PROGRAMMING IS
ESSENTIALLY WORKING WITH
ASYNCHRONOUS DATA STREAMS
@fpillet
ABOUT REACTIVE EXTENSIONS
▸ Event-driven
▸ Asynchronous
▸ Functional
▸ Common patterns
▸ Observer
▸ Iterator
▸ Cross-platform
CANONICAL EXAMPLE
IMPERATIVE
a = 3
b = 4
c = a + b // 7
a = 6
c // 7
CANONICAL EXAMPLE
REACTIVE
a = 3
b = 4
c = a + b
a = 6
c // 10
// 7
TRADITIONAL VS. REACTIVE
A SIMPLE EXAMPLE IN IOS
MODEL
VIEW MODEL
VIEW CONTROLLER
REACTIVE VIEW MODEL
REACTIVE VIEW CONTROLLER
THE RESULTS
40% LESS
Environment
Interactive
Reactive
Program
REACTIVE EXTENSIONS
N
ov.‘09
Rx.N
ET
RxJS
M
ar.‘10
RxJava
M
ar.‘12
RxC
pp
N
ov.‘12
RxRuby
D
ec.‘12
RxScala,RxC
lojure,RxG
roovy,RxJRuby
Jan.‘13
RxPY,RxPH
P
M
ar.‘13
RxKotlin
O
ct.‘13
RxSw
ift
Feb.‘15
REACTIVE MANIFESTO
RX STANDARDIZES THE
PATTERNS & OPERATORS
USED TO DEVELOP
ASYNCHRONOUS APPS IN
MULTIPLE
ENVIRONMENTS.
RXSWIFT OPERATORS
▸ Creating
asObservable
create
deferred
empty
error
toObservable
interval
never
just
of
range
repeatElement
timer
▸ Transforming
buffer
flatMap
flatMapFirst
flatMapLatest
map
scan
window
▸ Filtering
debounce / throttle
distinctUntilChanged
elementAt
filter
sample
skip
take
takeLast
single
▸ Conditional & Boolean
amb
skipWhile
skipUntil
takeUntil
takeWhile
▸ Mathematical &
Aggregate
concat
reduce
toArray
▸ Connectable
multicast
publish
refCount
replay
shareReplay
▸ Combining
merge
startWith
switchLatest
combineLatest
zip
▸ Error Handling
catch
retry
retryWhen
▸ Observing
delaySubscription
do / doOnNext
observeOn
subscribe
subscribeOn
timeout
using
debug
LIFECYCLE OF AN OBSERVABLE (AKA SEQUENCE)
▸ onNext
▸ onError
▸ onComplete
▸ dispose() / DisposeBag
DO YOU HAZ TEH
CODEZ ?!?
SETUP
▸ Install ThisCouldBeUsButYouPlaying
▸ bit.ly/podsPlaygrounds
▸ gem install cocoapods-playgrounds
▸ Create RxSwiftPlayground
▸ pod playgrounds RxSwift
SETUP
//: Please build the scheme 'RxSwiftPlayground' first
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import RxSwift
SETUP
//: Please build the scheme 'RxSwiftPlayground' first
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import RxSwift
func exampleOf(description: String, action: Void -> Void) {
print("n--- Example of:", description, "---")
action()
}
--- Example of: just ---
Next(32)
Completed
exampleOf("just") {
Observable.just(32)
.subscribe {
print($0)
}
}
CREATING & SUBSCRIBING
--- Example of: just ---
Next(32)
Completed
exampleOf("just") {
Observable.just(32)
.subscribe { value in
print(value)
}
}
CREATING & SUBSCRIBING
--- Example of: just ---
32
exampleOf("just") {
_ = Observable.just(32).subscribeNext { print($0) }
}
CREATING & SUBSCRIBING
--- Example of: of ---
1
2
3
4
5
exampleOf("of") {
Observable.of(1, 2, 3, 4, 5)
.subscribeNext {
print($0)
}
.dispose()
}
CREATING & SUBSCRIBING
--- Example of: toObservable ---
1
2
3
exampleOf("toObservable") {
let disposeBag = DisposeBag()
[1, 2, 3].toObservable()
.subscribeNext {
print($0)
}
.addDisposableTo(disposeBag)
}
CREATING & SUBSCRIBING
--- Example of: BehaviorSubject ---
Next(Hello)
Next(World!)
exampleOf("BehaviorSubject") {
let disposeBag = DisposeBag()
let string = BehaviorSubject(value: "Hello")
string.subscribe {
print($0)
}
.addDisposableTo(disposeBag)
string.onNext("World!")
}
CREATING & SUBSCRIBING
CREATING & SUBSCRIBING
--- Example of: Variable ---
Next(1)
Next(12)
Next(1234567)
Completed
exampleOf("Variable") {
let disposeBag = DisposeBag()
let number = Variable(1)
number.asObservable()
.subscribe {
print($0)
}
.addDisposableTo(disposeBag)
number.value = 12
number.value = 1_234_567
}
exampleOf("map") {
Observable.of(1, 2, 3)
.map { $0 * $0 }
.subscribeNext { print($0) }
.dispose()
}
TRANSFORMING
--- Example of: map ---
1
4
9
TRANSFORMING
--- Example of: Variable and map ---
one
twelve
one million two hundred thirty-four thousand five hundred sixty-seven
exampleOf("Variable and map") {
let disposeBag = DisposeBag()
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .SpellOutStyle
let number = Variable(1)
number.asObservable()
.map { numberFormatter.stringFromNumber($0)! }
.subscribeNext { print($0) }
.addDisposableTo(disposeBag)
number.value = 12
number.value = 1_234_567
}
TRANSFORMING
--- Example of: flatMap ---
Scott
Lori
Eric
exampleOf("flatMap") {
let disposeBag = DisposeBag()
struct Person {
var name: Variable<String>
}
let scott = Person(name: Variable("Scott"))
let lori = Person(name: Variable("Lori"))
let person = Variable(scott)
person.asObservable()
.flatMap {
$0.name.asObservable()
}
.subscribeNext {
print($0)
}
.addDisposableTo(disposeBag)
person.value = lori
scott.name.value = "Eric"
}
TRANSFORMING
--- Example of: flatMapLatest ---
Scott
Lori
exampleOf("flatMap") {
let disposeBag = DisposeBag()
struct Person {
var name: Variable<String>
}
let scott = Person(name: Variable("Scott"))
let lori = Person(name: Variable("Lori"))
let person = Variable(scott)
person.asObservable()
.flatMapLatest {
$0.name.asObservable()
}
.subscribeNext {
print($0)
}
.addDisposableTo(disposeBag)
person.value = lori
scott.name.value = "Eric"
}
exampleOf("distinctUntilChanged") {
let disposeBag = DisposeBag()
let searchString = Variable("iOS")
searchString.asObservable()
.map { $0.lowercaseString }
.distinctUntilChanged()
.subscribeNext { print($0) }
.addDisposableTo(disposeBag)
searchString.value = "IOS"
searchString.value = "Rx"
searchString.value = "ios"
}
FILTERING
--- Example of: distinctUntilChanged ---
ios
rx
ios
exampleOf("combineLatest") {
let disposeBag = DisposeBag()
let number = PublishSubject<Int>()
let string = PublishSubject<String>()
Observable.combineLatest(number, string) { "($0) ($1)" }
.subscribeNext { print($0) }
.addDisposableTo(disposeBag)
number.onNext(1)
print("Nothing yet")
string.on(.Next("A"))
number.onNext(2)
string.onNext("B")
string.onNext("C")
}
COMBINING
--- Example of: combineLatest ---
Nothing yet
1 A
2 A
2 B
2 C
exampleOf("takeWhile") {
[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1].toObservable()
.takeWhile { $0 < 5 }
.subscribeNext { print($0) }
.dispose()
}
CONDITIONAL
--- Example of: takeWhile ---
1
2
3
4
MATHEMATICAL
--- Example of: reduce ---
1024
exampleOf("reduce") {
[1, 2, 4, 8, 16].toObservable()
.reduce(1, accumulator: *)
.subscribeNext { print($0) }
.dispose()
}
ERROR HANDLING
--- Example of: error ---
A
exampleOf("error") {
enum Error: ErrorType { case A }
Observable<Int>.error(Error.A)
.subscribeError {
// Handle error
print($0)
}
.dispose()
}
RXMARBLES
RXSWIFTPLAYER
OBJECTIONS
1. Dependency
2. Complexity
3. Performance
4. Debugging
5. Help
BENEFITS
1. Write less, do more
2. Avoid mutable state errors
3. Learn once, apply everywhere
4. Mix in or go all in
5. “It reads almost like a paragraph”
6. Easy to test and heavily tested
QUESTIONS?
WANT MORE RX?
▸ github.com/ReactiveX/RxSwift
▸ reactivex.io
▸ rxmarbles.com
▸ rxswift.slack.com
▸ rx-marin.com
▸ http://as.ync.io
▸ github.com/scotteg/RxSwiftPlayer
▸ github.com/Artsy/eidolon
THANK YOU!
Scott Gardner
@scotteg
as.ync.io
bit.ly/scottOnLyndaDotCom

More Related Content

What's hot

What's hot (20)

Webpack DevTalk
Webpack DevTalkWebpack DevTalk
Webpack DevTalk
 
FlutterでGraphQLを扱う
FlutterでGraphQLを扱うFlutterでGraphQLを扱う
FlutterでGraphQLを扱う
 
Spring boot
Spring bootSpring boot
Spring boot
 
React js
React jsReact js
React js
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Reactjs workshop (1)
Reactjs workshop (1)Reactjs workshop (1)
Reactjs workshop (1)
 
Json in Postgres - the Roadmap
 Json in Postgres - the Roadmap Json in Postgres - the Roadmap
Json in Postgres - the Roadmap
 
Spring Boot Actuator 2.0 & Micrometer
Spring Boot Actuator 2.0 & MicrometerSpring Boot Actuator 2.0 & Micrometer
Spring Boot Actuator 2.0 & Micrometer
 
React
React React
React
 
Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring Batch 2.0
Spring Batch 2.0Spring Batch 2.0
Spring Batch 2.0
 

Viewers also liked

Viewers also liked (6)

Functional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwiftFunctional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwift
 
RxSwift
RxSwiftRxSwift
RxSwift
 
Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
 
Functional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwiftFunctional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwift
 
RxSwift x APIKit
RxSwift x APIKitRxSwift x APIKit
RxSwift x APIKit
 
Introduction to Functional Reactive Programming
Introduction to Functional Reactive ProgrammingIntroduction to Functional Reactive Programming
Introduction to Functional Reactive Programming
 

Similar to Reactive programming with RxSwift

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 

Similar to Reactive programming with RxSwift (20)

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
"Kotlin и rx в android" Дмитрий Воронин (Avito)
"Kotlin и rx в android" Дмитрий Воронин  (Avito)"Kotlin и rx в android" Дмитрий Воронин  (Avito)
"Kotlin и rx в android" Дмитрий Воронин (Avito)
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
My Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API'sMy Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API's
 
JavaOne報告会 Java SE/JavaFX 編 - JJUG CCC 2010 Fall
JavaOne報告会 Java SE/JavaFX 編 - JJUG CCC 2010 FallJavaOne報告会 Java SE/JavaFX 編 - JJUG CCC 2010 Fall
JavaOne報告会 Java SE/JavaFX 編 - JJUG CCC 2010 Fall
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
Scala on Your Phone
Scala on Your PhoneScala on Your Phone
Scala on Your Phone
 
Reactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwiftReactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwift
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
No more promises lets RxJS 2 Edit
No more promises lets RxJS 2 EditNo more promises lets RxJS 2 Edit
No more promises lets RxJS 2 Edit
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Self documentedcode
Self documentedcodeSelf documentedcode
Self documentedcode
 
No More Promises! Let's RxJS!
No More Promises! Let's RxJS!No More Promises! Let's RxJS!
No More Promises! Let's RxJS!
 
JOOQ and Flyway
JOOQ and FlywayJOOQ and Flyway
JOOQ and Flyway
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

Reactive programming with RxSwift