Slide 1

Slide 1 text

RxJava Intro Functional reactive programming By Xavier Lepretre

Slide 2

Slide 2 text

What we want What we know

Slide 3

Slide 3 text

Imperative and synchronous var a = fetch(key); var b = transform(a); var c = transformAgain(b); return process(c);

Slide 4

Slide 4 text

Asynchronous with callbacks fetch(key, new Callback() { void onReceived(a) { transform(a, new Callback() { void onFinished(b) ... }); } });

Slide 5

Slide 5 text

Futures future = executor.submit(new Callback() { Object call() { return doLongExecution(); } }); a = future.get();

Slide 6

Slide 6 text

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));

Slide 7

Slide 7 text

Important classes

Slide 8

Slide 8 text

Observable - 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

Slide 9

Slide 9 text

Observer - will receive: - the objects - the errors - the completed signal - can subscribe to many observables

Slide 10

Slide 10 text

Subscription - a handle to an observer subscribed to an observable - call .unsubscribe() when you no longer need to receive

Slide 11

Slide 11 text

Action1 { void call(MyType obj); } - 1 because it takes 1 parameter - There are Action2 … Action9

Slide 12

Slide 12 text

Func1 { public NextType call(MyType obj); } - 1 because it takes 1 parameter - There are Func2 … Func9

Slide 13

Slide 13 text

Important operations

Slide 15

Slide 15 text

No content

Slide 17

Slide 17 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Advantages - Unsubscription handles removing callback - Method can describe a subset of workflow

Slide 21

Slide 21 text

Create your own: OnSubscribe<> - Change callbacks into objects in pipeline - Know when to call onCompleted() - subscribeOn() - Subscriptions.create() - AndroidSubscriptions. unsubscribeInUiThread

Slide 22

Slide 22 text

Pitfalls - Forgot to .subscribe() - subscriptionList.unsubscribe(). Do not reuse - forgot observeOn(AndroidSchedulers. mainThread()) - callbacks may not show as used if you use retrolambda

Slide 23

Slide 23 text

Code Example https://github.com/xavierlepretre/rx-example

Slide 24

Slide 24 text

- All code shown is on client - Retrofit creates Observables -

Slide 25

Slide 25 text

- 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