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
Rx Java introduction
Search
SingaSUG
July 29, 2015
Technology
0
110
Rx Java introduction
Talk from Xavier Lepretre
SingaSUG
July 29, 2015
Tweet
Share
More Decks by SingaSUG
See All by SingaSUG
Java/Spring and Node.JS side by side
singasug
0
190
Migrating to microservices (Carlos Queiroz)
singasug
0
210
Docker 101 - Mario Loriedo
singasug
0
250
Docker for Java/Spring developers
singasug
0
69
Cloud Foundry and Docker
singasug
0
180
Multi dimensional scaling with CouchBase
singasug
1
110
NoSql presentation from Clarence Tauro
singasug
2
180
Spring Websockets
singasug
0
180
migrating from JSP to AngularJS
singasug
0
2.5k
Other Decks in Technology
See All in Technology
Strands Agents & Bedrock AgentCoreを1分でおさらい
minorun365
PRO
6
290
Agent Development Kitで始める生成 AI エージェント実践開発
danishi
0
140
JAWS AI/ML #30 AI コーディング IDE "Kiro" を触ってみよう
inariku
3
350
React Server ComponentsでAPI不要の開発体験
polidog
PRO
0
160
Bet "Bet AI" - Accelerating Our AI Journey #BetAIDay
layerx
PRO
4
1.7k
リモートワークで心掛けていること 〜AI活用編〜
naoki85
0
130
UDDのススメ - 拡張版 -
maguroalternative
1
420
反脆弱性(アンチフラジャイル)とデータ基盤構築
cuebic9bic
3
170
【新卒研修資料】数理最適化 / Mathematical Optimization
brainpadpr
25
13k
OPENLOGI Company Profile for engineer
hr01
1
37k
Telemetry APIから学ぶGoogle Cloud ObservabilityとOpenTelemetryの現在 / getting-started-telemetry-api-with-google-cloud
k6s4i53rx
0
140
Amazon GuardDuty での脅威検出:脅威検出の実例から学ぶ
kintotechdev
0
100
Featured
See All Featured
KATA
mclloyd
32
14k
How GitHub (no longer) Works
holman
314
140k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
1k
GitHub's CSS Performance
jonrohan
1031
460k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.3k
A better future with KSS
kneath
239
17k
Code Reviewing Like a Champion
maltzj
524
40k
Music & Morning Musume
bryan
46
6.7k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
Six Lessons from altMBA
skipperchong
28
3.9k
Transcript
RxJava Intro Functional reactive programming By Xavier Lepretre
What we want What we know
Imperative and synchronous var a = fetch(key); var b =
transform(a); var c = transformAgain(b); return process(c);
Asynchronous with callbacks fetch(key, new Callback() { void onReceived(a) {
transform(a, new Callback() { void onFinished(b) ... }); } });
Futures future = executor.submit(new Callback() { Object call() { return
doLongExecution(); } }); a = future.get();
Nice to have, workflow description When done, do that fetch(key).then(a
-> transform(a)) .then(b -> transformAgain(b)) .then(c -> process(c)) .then(d -> use(d));
Important classes
Observable<MyType> - like an object(s) pipe - no limit on
number of objects piped - also pipes Throwable - signals when no more object, completed - can have many observers, like a multicast
Observer<MyType> - will receive: - the objects - the errors
- the completed signal - can subscribe to many observables
Subscription - a handle to an observer subscribed to an
observable - call .unsubscribe() when you no longer need to receive
Action1<MyType> { void call(MyType obj); } - 1 because it
takes 1 parameter - There are Action2 … Action9
Func1<MyType, NextType> { public NextType call(MyType obj); } - 1
because it takes 1 parameter - There are Func2 … Func9
Important operations
.map(new Func1<A,B>(){}) - works on an object traveling along the
pipe - transforms from one type to another - to be used when the transformation is synchronous - example: convert
None
.doOnNext(new Action1<A>(){}) - Works on a object from the pipe
- Leaves the object unchanged - Example: save in cache, log
None
.flatMap(Func1<A, Observable<B>>) - Works on an object traveling along the
pipe - Transforms from one type to another - To be used when the transformation is asynchronous - Example: network fetch, open dialog
None
Advantages - Unsubscription handles removing callback - Method can describe
a subset of workflow
Create your own: OnSubscribe<> - Change callbacks into objects in
pipeline - Know when to call onCompleted() - subscribeOn() - Subscriptions.create() - AndroidSubscriptions. unsubscribeInUiThread
Pitfalls - Forgot to .subscribe() - subscriptionList.unsubscribe(). Do not reuse
- forgot observeOn(AndroidSchedulers. mainThread()) - callbacks may not show as used if you use retrolambda
Code Example https://github.com/xavierlepretre/rx-example
- All code shown is on client - Retrofit creates
Observables -
- workflow pieces in methods: for dialog, no need to
know next step - .onErrorResumeNext() to avoid break on less important elements - .flatMap() to show and wait for dialog - .publish() and .connect() to share Observable - SimpleAlertDialogOperator to convert a dialog into Observable