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

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. Reactive Live Coding
    Piotr Wittchen

    View Slide

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

    View Slide

  3. What is Reactive Programming?

    View Slide

  4. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. 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);
    }
    });

    View Slide

  9. 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();
    }

    View Slide

  10. 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

    View Slide

  11. Let’s code!

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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/

    View Slide

  17. Reactive Live Coding
    Piotr Wittchen
    twitter.com/piotr_wittchen
    github.com/pwittchen
    [email protected]
    wittchen.biz.pl

    View Slide