Slide 1

Slide 1 text

Reactive Live Coding Piotr Wittchen

Slide 2

Slide 2 text

What is the presentation plan? ● Quick introduction to RxJava ● Some coding ● Summary

Slide 3

Slide 3 text

What is Reactive Programming?

Slide 4

Slide 4 text

ArrayList list = Arrays.asList( ); Imperative Programming 1 2 3 4 5 6 for(Integer n : list) { operate(n); } for(Integer n : list) { if(n % 2 == 0) { operate(n); } } Data is pulled from its source

Slide 5

Slide 5 text

Reactive Programming 1 2 3 4 5 6 2 4 6 filter(n % 2 == 0) Data is pushed to the stream

Slide 6

Slide 6 text

Observable.just( ) .filter(i -> i % 2 == 0) .subscribe(i -> { });4 Reactive Programming 1 2 3 4 5 6 2 4 6

Slide 7

Slide 7 text

Reactive Programming Observable.just(1,2,3,4,5,6) .filter(i -> i % 2 == 0) .subscribe(item -> System.out.println(item));

Slide 8

Slide 8 text

Reactive Programming Observable.just(1,2,3,4,5,6) .filter(new Func1() { @Override public Boolean call(Integer i) { return i % 2; } }) .subscribe(new Action1() { @Override public void call(Integer item) { System.out.println(item); } });

Slide 9

Slide 9 text

Important structures of RxJava public class Observable { Subscription subscribe(); // has maaany methods for stream operations ... } public class Subscriber implements Observer, Subscription { ... } public interface Observer { void onCompleted(); void onError(Throwable e); void onNext(T t); } public interface Subscription { void unsubscribe(); boolean isUnsubscribed(); }

Slide 10

Slide 10 text

Multi-threading with RxJava Observable.just(1,2,3,4,5,6) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1() { @Override public void call(Integer item) { System.out.println(item); } }); order of called methods matters

Slide 11

Slide 11 text

Let’s code!

Slide 12

Slide 12 text

Everything can be a stream! Observable is an abstraction! ● HTTP responses: Retrofit ● Changes in SQLite database: Sqlbrite ● Changes in SharedPreferences: rx-preferences and prefser ● Location changes: Android-ReactiveLocation ● Network changes: ReactiveNetwork ● Stream of sensor readings: ReactiveSensors ● Stream of appearing BLE Beacons: ReactiveBeacons ● Reactive UI widgets: RxBinding ● and many more! programmer decides how to implement an Observable

Slide 13

Slide 13 text

but... ...there is no silver bullet The Mythical Man-Month: Essays on Software Engineering - a book by Frederick Brooks interesting article available as *.pdf file at: http://worrydream.com/refs/Brooks-NoSilverBullet.pdf

Slide 14

Slide 14 text

...so when should we use RxJava? ● non-deterministic operations (we don’t know, when they will start or finish) ● long lasting operations ● processing large amount of data ● error prone operations

Slide 15

Slide 15 text

RxJava is powerful library, but it’s just another tool ● Don’t use it everywhere and use it wisely ● Choose tools, which are the most appropriate for your solution ● Avoid over-engineering ● Keep your app simple

Slide 16

Slide 16 text

Resources ● https://github.com/ReactiveX/RxJava/ ● https://github.com/ReactiveX/RxJava/wiki ● http://reactivex.io/ ● https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 - Introduction to RxJava you’ve been missing ● https://speakerdeck.com/jakewharton/demystifying-rxjava-subscribers-oredev-2015 ● https://speakerdeck.com/jakewharton/retrofit-and-rxjava-netflix-open-source-meetup-s02e02 ● https://www.youtube.com/watch?v=sTSQlYX5DU0 - What does it mean to be reactive? ● http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ ● http://blog.danlew.net/2014/09/22/grokking-rxjava-part-2/ ● http://blog.danlew.net/2014/09/30/grokking-rxjava-part-3/ ● http://mttkay.github.io/blog/2013/08/25/functional-reactive-programming-on-android-with-rxjava/ ● https://speakerdeck.com/benjchristensen ● http://rxmarbles.com/

Slide 17

Slide 17 text

Reactive Live Coding Piotr Wittchen twitter.com/piotr_wittchen github.com/pwittchen piotr@wittchen.biz.pl wittchen.biz.pl