Learn & Master ⚔️ the Basics of RxSwift in 10 Minutes

Sebastian Boldt
Mar 4, 2017 · 10 min read
Image for post
Image for post
Image for post
Image for post

1. Observable Sequences 🎞

let helloSequence = Observable.just("Hello Rx")let fibonacciSequence = Observable.from([0,1,1,2,3,5,8])let dictSequence = Observable.from([1:"Hello",2:"World"])
let helloSequence = Observable.of("Hello Rx")let subscription = helloSequence.subscribe { event in
print(event)
}
OUTPUT:
next("Hello Rx")
completed
let helloSequence = Observable.from(["H","e","l","l","o"])let subscription = helloSequence.subscribe { event in
switch event {
case .next(let value):
print(value)
case .error(let error):
print(error)
case .completed:
print("completed")
}
}
OUTPUT:
H e l l o
completed
// Creating a DisposeBag so subscribtion will be cancelled correctly
let bag = DisposeBag()
// Creating an Observable Sequence that emits a String value
let observable = Observable.just("Hello Rx!")
// Creating a subscription just for next events
let subscription = observable.subscribe (onNext:{
print($0)
})
// Adding the Subscription to a Dispose Bag
subscription.addDisposableTo(bag)

2. Subjects 📫

Publish: 0

Behaviour & Variable: 1

Replay: N

let bag = DisposeBag()
var publishSubject = PublishSubject<String>()
publishSubject.onNext("Hello")
publishSubject.onNext("World")
let subscription1 = publishSubject.subscribe(onNext:{
print($0)
}).addDisposableTo(bag)
// Subscription1 receives these 2 events, Subscription2 won't
publishSubject.onNext("Hello")
publishSubject.onNext("Again")
// Sub2 will not get "Hello" and "Again" because it susbcribed later
let subscription2 = publishSubject.subscribe(onNext:{
print(#line,$0)
})
publishSubject.onNext("Both Subscriptions receive this message")

3. Marble Diagrams 🙌🏼

Image for post
Image for post

4. Transformations ⚙️

Image for post
Image for post
Observable<Int>.of(1,2,3,4).map { value in 
return value * 10
}.subscribe(onNext:{
print($0)
})
OUTPUT: 10 20 30 40
Image for post
Image for post
let sequence1  = Observable<Int>.of(1,2)
let sequence2 = Observable<Int>.of(1,2)
let sequenceOfSequences = Observable.of(sequence1,sequence2)sequenceOfSequences.flatMap{ return $0 }.subscribe(onNext:{
print($0)
})
OUTPUT: 1 2 1 2
Image for post
Image for post
Observable.of(1,2,3,4,5).scan(0) { seed, value in
return seed + value
}.subscribe(onNext:{
print($0)
})
OUTPUT: 1 3 6 10 15
Image for post
Image for post
SequenceThatEmitsWithDifferentIntervals
.buffer(timeSpan: 150, count: 3, scheduler:s)
.subscribe(onNext:{
print($0)
})
OUTPUT: [1] [2,3] [4] [5,6] [7] []

5.Filter 🚬

Image for post
Image for post
Observable.of(2,30,22,5,60,1).filter{$0 > 10}.subscribe(onNext:{
print($0)
})
OUTPUT: 30 22 60
Image for post
Image for post
Observable.of(1,2,2,1,3).distinctUntilChanged().subscribe(onNext:{
print($0)
})
OUTPUT: 1 2 1 3

6. Combine 💑

Image for post
Image for post
Observable.of(2,3).startWith(1).subscribe(onNext:{
print($0)
})
OUTPUT: 1 2 3
Image for post
Image for post
let publish1 = PublishSubject<Int>()
let publish2 = PublishSubject<Int>()
Observable.of(publish1,publish2).merge().subscribe(onNext:{
print($0)
})
publish1.onNext(20)
publish1.onNext(40)
publish1.onNext(60)
publish2.onNext(1)
publish1.onNext(80)
publish2.onNext(2)
publish1.onNext(100)
OUTPUT: 20 40 60 1 80 2 100
Image for post
Image for post
let a = Observable.of(1,2,3,4,5)
let b = Observable.of("a","b","c","d")
Observable.zip(a,b){ return ($0,$1) }.subscribe {
print($0)
}
OUTPUT: (1, "a")(2, "b") (3, "c") (4, "d")

7. Side Effects 👉

Observable.of(1,2,3,4,5).do(onNext: {
$0 * 10 // This has no effect on the actual subscription
}).subscribe(onNext:{
print($0)
})

8.Schedulers ⏰

let publish1 = PublishSubject<Int>()
let publish2 = PublishSubject<Int>()
let concurrentScheduler = ConcurrentDispatchQueueScheduler(qos: .background)Observable.of(publish1,publish2)
.observeOn(concurrentScheduler)
.merge()
.subscribeOn(MainScheduler())
.subscribe(onNext:{
print($0)
})
publish1.onNext(20)
publish1.onNext(40)
OUTPUT: 20 40

It’s a wrap 🎁


iOS App Development

Stories and technical tips about building apps for iOS…

Sebastian Boldt

Written by

Creative Head, iOS Developer @ Immowelt, DJ/Producer from Hamburg. Creator of Jelly, an animation Framework written in Swift. https://www.sebastianboldt.com

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Sebastian Boldt

Written by

Creative Head, iOS Developer @ Immowelt, DJ/Producer from Hamburg. Creator of Jelly, an animation Framework written in Swift. https://www.sebastianboldt.com

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store