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
47
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
48
Arrow Meta 1o1
ffgiraldez
0
44
Don't Fear the Monads
ffgiraldez
1
140
Reactive your Android
ffgiraldez
1
140
Other Decks in Programming
See All in Programming
MCPとデザインシステムに立脚したデザインと実装の融合
yukukotani
4
1.4k
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
430
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
100
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
サーバーサイドのビルド時間87倍高速化
plaidtech
PRO
0
720
アセットのコンパイルについて
ojun9
0
120
ProxyによるWindow間RPC機構の構築
syumai
3
1.1k
アプリの "かわいい" を支えるアニメーションツールRiveについて
uetyo
0
210
1から理解するWeb Push
dora1998
7
1.8k
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
知っているようで知らない"rails new"の世界 / The World of "rails new" You Think You Know but Don't
luccafort
PRO
1
100
Featured
See All Featured
Fireside Chat
paigeccino
39
3.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
4 Signs Your Business is Dying
shpigford
184
22k
GitHub's CSS Performance
jonrohan
1032
460k
Become a Pro
speakerdeck
PRO
29
5.5k
Visualization
eitanlees
148
16k
Typedesign – Prime Four
hannesfritz
42
2.8k
Building Adaptive Systems
keathley
43
2.7k
Making Projects Easy
brettharned
117
6.4k
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]