Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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

Piotr Wittchen

November 28, 2015
Tweet

More Decks by Piotr Wittchen

Other Decks in Programming

Transcript

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

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

    -> { });4 Reactive Programming 1 2 3 4 5 6 2 4 6
  4. 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); } });
  5. 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(); }
  6. 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
  7. 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
  8. ...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
  9. 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
  10. 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/