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
86
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
160
Migrating to microservices (Carlos Queiroz)
singasug
0
190
Docker 101 - Mario Loriedo
singasug
0
200
Docker for Java/Spring developers
singasug
0
54
Cloud Foundry and Docker
singasug
0
130
Multi dimensional scaling with CouchBase
singasug
1
88
NoSql presentation from Clarence Tauro
singasug
2
140
Spring Websockets
singasug
0
160
migrating from JSP to AngularJS
singasug
0
2.3k
Other Decks in Technology
See All in Technology
Creative UIs with Compose: DroidKaigi 2024
chrishorner
1
600
Fediverse Discovery Providers overview
andypiper
0
170
eBPFのこれまでとこれから
yutarohayakawa
10
3.3k
『GRANBLUE FANTASY: Relink』最高の「没入感」を実現するカットシーン制作手法とそれを支える技術
cygames
1
150
テスト”ケース”駆動開発 で手戻りをなくそう
ryohma0510
0
330
フルカイテン株式会社 採用資料
fullkaiten
0
32k
「認証認可」という体験をデザインする ~Nekko Cloud認証認可基盤計画
logica0419
2
450
OCI で始める!! Red Hat OpenShift / Get Started OpenShift on OCI
oracle4engineer
PRO
1
190
Developer Experienceを向上させる基盤づくりの取り組み事例集
coconala_engineer
0
160
JTCや セキュリティチェックリストが夢の跡
nikinusu
1
760
DuckDB雑紹介(1.1対応版)@DuckDB座談会
ktz
6
1.4k
Kubernetesって何? -大規模なKubernetesを運用するKubernetes as a Serviceチームの話を添えて-
lycorptech_jp
PRO
0
140
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.8k
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
Navigating Team Friction
lara
183
13k
Facilitating Awesome Meetings
lara
49
6k
Producing Creativity
orderedlist
PRO
340
39k
Ruby is Unlike a Banana
tanoku
96
11k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
24
610
Rails Girls Zürich Keynote
gr2m
93
13k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
1
55
Statistics for Hackers
jakevdp
794
220k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.6k
How GitHub (no longer) Works
holman
310
140k
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