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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Fernando Franco Gíraldez
December 01, 2021
Programming
50
0
Share
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
More Decks by Fernando Franco Gíraldez
See All by Fernando Franco Gíraldez
Design for Developer
ffgiraldez
0
51
Arrow Meta 1o1
ffgiraldez
0
49
Don't Fear the Monads
ffgiraldez
1
160
Reactive your Android
ffgiraldez
1
140
Other Decks in Programming
See All in Programming
How Swift's Type System Guides AI Agents
koher
0
290
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
240
Don't Prompt Harder, Structure Better
kitasuke
0
780
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
390
運転動画を検索可能にする〜Cosmos-Embed1とDatabricks Vector Searchで〜/cosmos-embed1-databricks-vector-search
studio_graph
0
380
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
210
事業会社でのセキュリティ長期インターンについて
masachikaura
0
260
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
280
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
iOS機能開発のAI環境と起きた変化
ryunakayama
0
190
第3木曜LT会 #28
tinykitten
PRO
0
110
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
170
Featured
See All Featured
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
100
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
Fireside Chat
paigeccino
42
3.9k
Exploring anti-patterns in Rails
aemeredith
3
320
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Building Adaptive Systems
keathley
44
3k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Speed Design
sergeychernyshev
33
1.6k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
150
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
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]