Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rx Java introduction

Rx Java introduction

Talk from Xavier Lepretre

SingaSUG

July 29, 2015
Tweet

More Decks by SingaSUG

Other Decks in Technology

Transcript

  1. Imperative and synchronous var a = fetch(key); var b =

    transform(a); var c = transformAgain(b); return process(c);
  2. Asynchronous with callbacks fetch(key, new Callback() { void onReceived(a) {

    transform(a, new Callback() { void onFinished(b) ... }); } });
  3. 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));
  4. 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
  5. Observer<MyType> - will receive: - the objects - the errors

    - the completed signal - can subscribe to many observables
  6. Subscription - a handle to an observer subscribed to an

    observable - call .unsubscribe() when you no longer need to receive
  7. Action1<MyType> { void call(MyType obj); } - 1 because it

    takes 1 parameter - There are Action2 … Action9
  8. Func1<MyType, NextType> { public NextType call(MyType obj); } - 1

    because it takes 1 parameter - There are Func2 … Func9
  9. .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
  10. .doOnNext(new Action1<A>(){}) - Works on a object from the pipe

    - Leaves the object unchanged - Example: save in cache, log
  11. .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
  12. Create your own: OnSubscribe<> - Change callbacks into objects in

    pipeline - Know when to call onCompleted() - subscribeOn() - Subscriptions.create() - AndroidSubscriptions. unsubscribeInUiThread
  13. Pitfalls - Forgot to .subscribe() - subscriptionList.unsubscribe(). Do not reuse

    - forgot observeOn(AndroidSchedulers. mainThread()) - callbacks may not show as used if you use retrolambda
  14. - 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