Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Intro to reactive programing
Search
Fernando Franco Gíraldez
December 01, 2021
Programming
0
45
Intro to reactive programing
Small introduction into rx-java 2.X and it's core element and how to use it
Fernando Franco Gíraldez
December 01, 2021
Tweet
Share
More Decks by Fernando Franco Gíraldez
See All by Fernando Franco Gíraldez
Design for Developer
ffgiraldez
0
47
Arrow Meta 1o1
ffgiraldez
0
42
Don't Fear the Monads
ffgiraldez
1
140
Reactive your Android
ffgiraldez
1
140
Other Decks in Programming
See All in Programming
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
170
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
320
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
44
30k
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
1
470
プロダクト志向なエンジニアがもう一歩先の価値を目指すために意識したこと
nealle
0
110
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
16
3.1k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
200
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
250
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
980
技術同人誌をMCP Serverにしてみた
74th
0
270
Go1.25からのGOMAXPROCS
kuro_kurorrr
1
800
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
240
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
462
33k
BBQ
matthewcrist
89
9.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Designing for Performance
lara
609
69k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Site-Speed That Sticks
csswizardry
10
650
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Unsuck your backbone
ammeep
671
58k
The Language of Interfaces
destraynor
158
25k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Transcript
Intro To Reactive programing
Schedule ▸ What is reactive programming ▸ Core elements of
rx-java ▸ How to test your reactive chains 2
“ The world has move to push; user are waiting
for us to catch up Lee Campbell - Introduction to Rx
1. What is reactive programing A brief description 4
Mixing of ideas Observable Pattern Iterable Pattern FP style 5
What is reactive programing It’s a library for composing asynchronous
and event based programs by using observable sequences 6
2. Core elements 7
Core elements \ concepts ▸ Observable ▸ Operators ▸ Observer
▸ Disposable ▸ Threading 8
Core elements \ events ▸ onNext ▸ onComplete ▸ onError
9
Core elements \ Observable Observable.create { emitter -> try {
if (!emitter.isDisposed) { emitter.onNext("rxjava FTW!") emitter.onNext("functional programing FTW!") emitter.onComplete() } } catch (exception: Exception) { emitter.onError(exception) } } 10
Core elements \ Observable Observable.create { emitter -> val receiver
= object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) = emitter.onNext(intent) } context.registerReceiver(receiver, IntentFilter()) emitter.setCancellable { context.unregisterReceiver(receiver) } } 11
Core elements \ Observable \ Specialization ▸ Observable ▸ Single
▸ Completable ▸ Maybe ▸ Flowable 12
Core elements \ Operators 13
Core elements \ Operators 14 networkRequestSingle.map { response -> response.result.name
}
Core elements \ Operators 15 textWatcherObservable.debounce(400, TimeUnit.MILLISECONDS)
Core elements \ Operators 16 suggestionTextWatcher.debounce(400, TimeUnit.MILLISECONDS) .switchMapSingle { repo.searchSuggestion(it)
}
Core elements \ Operators 17 suggestionTextWatcher.doOnNext { loading.postValue(true) } .switchMapSingle
{ repo.searchSuggestion(it) } .doOnSubscribe { loading.postValue(false) }
Core elements \ Observer val observer = object: Observer<String>{ override
fun onNext(t: String) {} override fun onError(e: Throwable) {} override fun onComplete() {} override fun onSubscribe(d: Disposable) {} } 18
Core elements \ Observer suggestionTextWatcher .switchMapSingle { repo.searchSuggestion(it) } .subscribe(observer)
.subscribe { results.postValue(it) } .subscribe({ results -> }, { throwable -> }) .subscribe({ results -> }, { throwable -> }, {}) .subscribeBy( onNext = { results -> }, onError = { throwable -> }, onComplete = {} ) 19
Core elements \ Disposable val disposable = suggestionTextWatcher .subscribe {
results.postValue(it) } disposable.isDisposed() disposable.dispose() val compositeDisposable = CompositeDisposable() disposable.add(suggestionTextWatcher.subscribe{}) disposable.clear() 20
suggestionTextWatcher.switchMapSingle { repo.searchSuggestion(it) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) Core elements \ Threading
21 To add concurrency into our observable sequence, just indicate to the observable to operate in a particular Scheduler
suggestionTextWatcher.switchMapSingle { repo.searchSuggestion(it) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) Core elements \ Threading
22 Specify the Scheduler on which an Observable will operate.
suggestionTextWatcher.switchMapSingle { repo.searchSuggestion(it) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) Core elements \ Threading
23 Specify the Scheduler on which an Observer will observe this observable.
Testing ▸ How to provide Schedulers? ▸ Make test run
faster ▸ Use black box testing strategies 24
Testing \ Schedulers val testScheduler = TestScheduler() RxJavaPlugins.setIoSchedulerHandler( scheduler ->
testScheduler); RxAndroidPlugins.setMainThreadSchedulerHandler( scheduler -> Schedulers.trampoline()); @field:Rule val testSchedulers = TestSchedulerRule.create(); testScheduler.advanceTimeBy(30, TimeUnit.SECONDS); 25
Testing \ TestObserver Val testObserver = voiceRecognition.observe().test(); testObserver.assertValues(....); testObserver.isTerminated() testObserver.assertSubscribed()
26
27 THANKS! Any questions? @thanerian
[email protected]