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
82
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
180
Docker 101 - Mario Loriedo
singasug
0
180
Docker for Java/Spring developers
singasug
0
52
Cloud Foundry and Docker
singasug
0
110
Multi dimensional scaling with CouchBase
singasug
1
85
NoSql presentation from Clarence Tauro
singasug
2
140
Spring Websockets
singasug
0
130
migrating from JSP to AngularJS
singasug
0
2.2k
Other Decks in Technology
See All in Technology
Azure犬駆動開発の記録/GlobalAzureFukuoka2024_20240420
nina01
1
230
LLM開発・活用の舞台裏@2024.04.25
yushin_n
3
1k
コードや知識を組み込む / Incorporate Code and knowledge
ks91
PRO
0
120
アクセス制御にまつわる改善 / Improving access control
itkq
0
580
エンジニア候補者向け資料2024.04.24.pdf
macloud
0
3.3k
20分で完全に理解するGrafanaダッシュボード
hamadakoji
4
830
いつか使うかも貯金してたらめちゃめちゃ機能が増えてた話
riyaamemiya
0
490
Cypress or Playwright?
rainerhahnekamp
0
150
R3のコードから見る実践LINQ実装最適化・コンカレントプログラミング実例
neuecc
3
1.4k
本当のAWS基礎
toru_kubota
1
560
Gradle Build Scanを使ってビルドのことを知ろう potatotips #87
tomorrowkey
2
140
DMM.com アルファ室採用案内資料
hsugita
1
200
Featured
See All Featured
Statistics for Hackers
jakevdp
790
220k
Documentation Writing (for coders)
carmenintech
61
4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Being A Developer After 40
akosma
64
580k
Building Better People: How to give real-time feedback that sticks.
wjessup
356
18k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
12
8.3k
Building Effective Engineering Teams - LeadDev
addyosmani
30
1.9k
We Have a Design System, Now What?
morganepeng
44
6.8k
BBQ
matthewcrist
80
8.8k
Side Projects
sachag
451
41k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
10
1k
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