Reactive Live Coding

Reactive Live Coding

Presentation covering basics of Reactive Programming, RxJava and RxAndroid prepared for GDG DevFest 2015 in Warsaw, Poland (http://devfest.pl). It was also presented during Gliwice Software Barcamp in January 2016 and Silesian Java Users Group (SJUG) meeting in Katowice in February 2016. Source code of the sample Android app shown and partially coded during the presentation is available at: https://github.com/pwittchen/guitar-browser

E8bb35e5d8c868e7dffcd9859248d6f1?s=128

Piotr Wittchen

November 28, 2015
Tweet

Transcript

  1. Reactive Live Coding Piotr Wittchen

  2. What is the presentation plan? • Quick introduction to RxJava

    • Some coding • Summary
  3. What is Reactive Programming?

  4. ArrayList<Integer> 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
  5. Reactive Programming 1 2 3 4 5 6 2 4

    6 filter(n % 2 == 0) Data is pushed to the stream
  6. Observable.just( ) .filter(i -> i % 2 == 0) .subscribe(i

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

    .subscribe(item -> System.out.println(item));
  8. Reactive Programming Observable.just(1,2,3,4,5,6) .filter(new Func1<Integer, Boolean>() { @Override public Boolean

    call(Integer i) { return i % 2; } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer item) { System.out.println(item); } });
  9. Important structures of RxJava public class Observable<T> { Subscription subscribe();

    // has maaany methods for stream operations ... } public class Subscriber<T> implements Observer<T>, Subscription { ... } public interface Observer<T> { void onCompleted(); void onError(Throwable e); void onNext(T t); } public interface Subscription { void unsubscribe(); boolean isUnsubscribed(); }
  10. Multi-threading with RxJava Observable.just(1,2,3,4,5,6) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Integer>() { @Override

    public void call(Integer item) { System.out.println(item); } }); order of called methods matters
  11. Let’s code!

  12. 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
  13. 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
  14. ...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
  15. 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
  16. 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/
  17. Reactive Live Coding Piotr Wittchen twitter.com/piotr_wittchen github.com/pwittchen piotr@wittchen.biz.pl wittchen.biz.pl