$30 off During Our Annual Pro Sale. View Details »

Looking Ahead To RxJava 2 (Droidcon NYC 2016)

Looking Ahead To RxJava 2 (Droidcon NYC 2016)

Development on the next major version of RxJava is underway. While the operators remain largely unchanged, Observable creation, subscription management, and backpressure has been completely overhauled in this new version. This talk will be an exploration on not only what has changed but also the reasons behind why these changes were made. We'll also talk about how both libraries and applications can migrate to supporting RxJava 2 as well as interop between the two versions. No prior knowledge of RxJava is strictly required, although concepts, terms, and comparisons will be made that do assume some prior knowledge.

Video: soon

Jake Wharton
PRO

September 29, 2016
Tweet

More Decks by Jake Wharton

Other Decks in Programming

Transcript

  1. Looking Ahead To

    RxJava 2
    Jake Wharton

    View Slide

  2. Reactive Streams
    ...is an initiative to provide a standard
    for asynchronous stream processing
    with non-blocking back pressure.

    View Slide

  3. Reactive Streams
    interface Publisher {

    void subscribe(Subscriber super T> s);

    }A

    View Slide

  4. Reactive Streams
    interface Publisher {

    void subscribe(Subscriber super T> s);

    }A
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    }B

    View Slide

  5. Reactive Streams
    interface Publisher {

    void subscribe(Subscriber super T> s);

    }A
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B

    View Slide

  6. Reactive Streams
    interface Publisher {

    void subscribe(Subscriber super T> s);

    }A
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Subscription {

    void request(long n);

    void cancel();

    }C

    View Slide

  7. Reactive Streams
    interface Publisher {

    void subscribe(Subscriber super T> s);

    }A
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Subscription {

    void request(long n);

    void cancel();

    }C
    interface Processor extends Subscriber, Publisher {

    }D


    View Slide

  8. Reactive Streams
    class RxReactiveStreams {

    static Publisher toPublisher(Observable o) { … }

    static Observable toObservable(Publisher p) { … }

    static Publisher toPublisher(Single o) { … }

    static Single toSingle(Publisher p) { … }

    static Publisher toPublisher(Completable o) { … }

    static Completable toCompletable(Publisher> p) { … }

    }

    View Slide

  9. Reactive Streams
    class RxReactiveStreams {

    static Publisher toPublisher(Observable o) { … }

    static Observable toObservable(Publisher p) { … }

    static Publisher toPublisher(Single o) { … }

    static Single toSingle(Publisher p) { … }

    static Publisher toPublisher(Completable o) { … }

    static Completable toCompletable(Publisher> p) { … }

    }
    github.com/ReactiveX/RxJavaReactiveStreams

    View Slide

  10. Reactive Streams
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B

    View Slide

  11. Reactive Streams
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    Calling onSubscribe, onNext, onError or
    onComplete MUST return normally

    View Slide

  12. Reactive Streams
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    Calling onSubscribe, onNext, onError or
    onComplete MUST return normally except when any
    provided parameter is null in which case it MUST throw
    a java.lang.NullPointerException to the caller

    View Slide

  13. Reactive Streams
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    Calling onSubscribe, onNext, onError or
    onComplete MUST return normally except when any
    provided parameter is null in which case it MUST throw
    a java.lang.NullPointerException to the caller

    View Slide

  14. Flowable

    View Slide

  15. Flowable
    ALL
    NEW!!!!!

    View Slide

  16. Flowable
    • Implementation of Reactive Streams Publisher.
    ALL
    NEW!!!!!

    View Slide

  17. Flowable
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    ALL
    NEW!!!!!

    View Slide

  18. Flowable
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    • 0 to many items, completes or errors.
    ALL
    NEW!!!!!

    View Slide

  19. Flowable
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    • 0 to many items, completes or errors.
    • Handles backpressure to slow down events

    with Reactive Streams Subscription.
    ALL
    NEW!!!!!

    View Slide

  20. Flowable
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    • 0 to many items, completes or errors.
    • Handles backpressure to slow down events

    with Reactive Streams Subscription.
    • Hundreds of convenience methods for

    transforming and composing data.
    ALL
    NEW!!!!!

    View Slide

  21. Flowable
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    • 0 to many items, completes or errors.
    • Handles backpressure to slow down events

    with Reactive Streams Subscription.
    • Hundreds of convenience methods for

    transforming and composing data.
    ALL
    NEW!!!!!

    View Slide

  22. Flowable (aka RxJava 1.x's Observable)
    • Implementation of Reactive Streams Publisher.
    • Observed with Reactive Streams Subscriber.
    • 0 to many items, completes or errors.
    • Handles backpressure to slow down events

    with Reactive Streams Subscription.
    • Hundreds of convenience methods for

    transforming and composing data.

    View Slide

  23. FlowableProcessor

    View Slide

  24. FlowableProcessor
    ALL
    NEW!!!!!

    View Slide

  25. FlowableProcessor
    • Implementation of Reactive Streams Processor

    (aka Publisher via Flowable + Subscriber).
    ALL
    NEW!!!!!

    View Slide

  26. FlowableProcessor
    • Implementation of Reactive Streams Processor

    (aka Publisher via Flowable + Subscriber).
    ALL
    NEW!!!!!

    View Slide

  27. FlowableProcessor (aka 1.x's Subject)
    • Implementation of Reactive Streams Processor

    (aka Publisher via Flowable + Subscriber).

    View Slide

  28. Observable? Subject?

    View Slide

  29. Observable
    • Observed with an Observer (not Reactive Streams type).
    • 0 to many items, completes or errors.
    • No backpressure support.
    • Hundreds of convenience methods for transforming and

    composing data.

    View Slide

  30. Observable
    • Observed with an Observer (not Reactive Streams type).
    • 0 to many items, completes or errors.
    • No backpressure support.
    • Hundreds of convenience methods for transforming and

    composing data.

    View Slide

  31. Subject
    • Implementation of Observable and Observer.

    View Slide

  32. Flowable vs. Observable

    View Slide

  33. Flowable vs. Observable
    • RxJava 1.x added backpressure late in the design process.

    View Slide

  34. Flowable vs. Observable
    • RxJava 1.x added backpressure late in the design process.
    • All types exposed backpressure but not all sources respected it.

    View Slide

  35. Flowable vs. Observable
    • RxJava 1.x added backpressure late in the design process.
    • All types exposed backpressure but not all sources respected it.
    • Backpressure, like inheritance, must be designed for.

    View Slide

  36. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));

    View Slide

  37. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));

    View Slide

  38. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));
    db.createQuery("SELECT * …")
    .subscribe(e -> update(e));

    View Slide

  39. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));
    db.createQuery("SELECT * …")
    .subscribe(e -> update(e));

    View Slide

  40. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));
    db.createQuery("SELECT * …")
    .subscribe(e -> update(e));
    Observable Observable

    View Slide

  41. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));
    db.createQuery("SELECT * …")
    .subscribe(e -> update(e));
    Observable Observable
    MissingBackpressureException

    View Slide

  42. Flowable vs. Observable
    • Backpressure, like inheritance, must be designed for.
    RxView.touches(paintView)
    .subscribe(e -> draw(e));
    db.createQuery("SELECT * …")
    .subscribe(e -> update(e));
    Observable Flowable

    View Slide

  43. Flowable vs. Observable
    Observable Flowable
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Observer {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Disposable d);

    }B

    View Slide

  44. Flowable vs. Observable
    Observable Flowable
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Observer {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Disposable d);

    }B
    interface Disposable {

    void dispose();

    }B
    interface Subscription {

    void cancel();

    void request(long r);

    }B

    View Slide

  45. Converting

    View Slide

  46. Converting
    Observable

    View Slide

  47. Converting
    1 2 3 4 5 6 7 8 9 10 11
    Observable

    View Slide

  48. Converting
    Observable
    toFlowable()X

    View Slide

  49. Converting
    Observable
    toFlowable(DROP)X

    View Slide

  50. Converting
    Observable
    toFlowable(DROP)X
    Flowable

    View Slide

  51. Converting
    Observable
    Flowable
    toFlowable(DROP)X
    request(5)

    View Slide

  52. Converting
    1 2 3 4 5
    Observable
    Flowable
    toFlowable(DROP)X
    1 2 3 4 5
    request(5)

    View Slide

  53. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable
    toFlowable(DROP)X
    1 2 3 4 5
    request(5)

    View Slide

  54. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable
    toFlowable(DROP)X
    1 2 3 4 5
    request(5) request(2)

    View Slide

  55. Converting
    1 2 3 4 5 6 7 8 9 10 11
    Observable
    Flowable
    toFlowable(DROP)X
    1 2 3 4 5 9 10
    request(5) request(2)

    View Slide

  56. Converting
    Observable
    toFlowable(LATEST)X
    Flowable

    View Slide

  57. Converting
    Observable
    Flowable
    request(5)
    toFlowable(LATEST)X

    View Slide

  58. Converting
    1 2 3 4 5
    Observable
    Flowable 1 2 3 4 5
    request(5)
    toFlowable(LATEST)X

    View Slide

  59. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5)
    toFlowable(LATEST)X

    View Slide

  60. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5) request(2)
    toFlowable(LATEST)X

    View Slide

  61. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5) request(2)
    toFlowable(LATEST)X
    8

    View Slide

  62. Converting
    1 2 3 4 5 6 7 8 9 10 11
    Observable
    Flowable 1 2 3 4 5 9
    request(5) request(2)
    toFlowable(LATEST)X
    8

    View Slide

  63. Converting
    Observable
    BUFFER
    Flowable
    toFlowable( )X

    View Slide

  64. Converting
    Observable
    Flowable
    request(5)
    toFlowable(BUFFER)X

    View Slide

  65. Converting
    1 2 3 4 5
    Observable
    Flowable 1 2 3 4 5
    request(5)
    toFlowable(BUFFER)X

    View Slide

  66. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5)
    toFlowable(BUFFER)X

    View Slide

  67. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5) request(2)
    toFlowable(BUFFER)X

    View Slide

  68. Converting
    1 2 3 4 5 6 7 8
    Observable
    Flowable 1 2 3 4 5
    request(5) request(2)
    toFlowable(BUFFER)X
    6 7

    View Slide

  69. Converting
    1 2 3 4 5 6 7 8 9 10 11
    Observable
    Flowable 1 2 3 4 5
    request(5) request(2)
    toFlowable(BUFFER)X
    6 7

    View Slide

  70. Converting
    toObservable()
    Observable
    Flowable

    View Slide

  71. Converting
    Observable
    Flowable
    request(Long.MAX_VALUE)
    toObservable()

    View Slide

  72. Converting
    1 2 3 4 5 6 7 8 9 10 11
    Observable
    Flowable
    1 2 3 4 5
    request(Long.MAX_VALUE)
    6 7 8 9 10 11
    toObservable()

    View Slide

  73. Specializations

    View Slide

  74. Specializations
    • Encoding subsets of Observable into the type system.

    View Slide

  75. Single

    View Slide

  76. Single
    • Either succeeds with an item or errors.
    • No backpressure support.
    • Convenience methods for transforming and composing data.

    View Slide

  77. Single
    • Either succeeds with an item or errors.
    • No backpressure support.
    • Convenience methods for transforming and composing data.
    • Think "reactive scalar".

    View Slide

  78. Completable

    View Slide

  79. Completable
    • Either completes or errors. Has no items!
    • No backpressure support.
    • Convenience methods for transforming and composing data.

    View Slide

  80. Completable
    • Either completes or errors. Has no items!
    • No backpressure support.
    • Convenience methods for transforming and composing data.
    • Think "reactive runnable".

    View Slide

  81. Maybe

    View Slide

  82. Maybe
    ALL
    NEW!!!!!

    View Slide

  83. Maybe
    • Either succeeds with an item, completes with no items, or errors.
    • No backpressure support.
    • Convenience methods for transforming

    and composing data. ALL
    NEW!!!!!

    View Slide

  84. Maybe
    • Either succeeds with an item, completes with no items, or errors.
    • No backpressure support.
    • Convenience methods for transforming

    and composing data.
    • Think "reactive optional".
    ALL
    NEW!!!!!

    View Slide

  85. Specializations
    • Encoding subsets of Observable into the type system.
    • Single – Item or error. Think "scalar".
    • Completable – Complete or error. Think "runnable".
    • Maybe – Item, complete, or error. Think "optional".

    View Slide

  86. Backpressure

    & Reactive Streams
    No Backpressure
    0…n items, complete|error Flowable Observable
    item|complete|error Maybe
    item|error Single
    complete|error Completable
    Multicast FlowableProcessor Subject

    View Slide

  87. Operator Specialization

    View Slide

  88. Operator Specialization
    first()
    Observable

    View Slide

  89. Operator Specialization
    first()
    Observable
    Observable

    View Slide

  90. Operator Specialization
    1 2 3 4 5 6 7 8 9 10 11
    first()
    1
    Observable
    Observable

    View Slide

  91. Operator Specialization
    first()
    Observable
    Single

    View Slide

  92. Operator Specialization
    1 2 3 4 5 6 7 8 9 10 11
    first()
    1
    Observable
    Single

    View Slide

  93. Operator Specialization
    first()
    Observable
    Single
    NoSuchElementException

    View Slide

  94. Operator Specialization
    firstElement()
    Observable

    View Slide

  95. Operator Specialization
    firstElement()
    Observable
    Maybe

    View Slide

  96. Operator Specialization
    firstElement()
    Observable
    Maybe

    View Slide

  97. Operator Specialization
    ignoreElements()
    Observable

    View Slide

  98. Operator Specialization
    ignoreElements()
    Observable
    Completable

    View Slide

  99. Operator Specialization
    1 2 3 4 5 6 7 8 9
    ignoreElements()
    Observable
    Completable

    View Slide

  100. Operator Specialization
    ignoreElements()
    Flowable
    Completable

    View Slide

  101. Operator Specialization
    1 2 3 4 5 6 7 8 9
    ignoreElements()
    Flowable
    Completable

    View Slide

  102. Operator Specialization
    firstElement()
    Flowable
    Maybe

    View Slide

  103. Operator Specialization
    first()
    Flowable
    Single

    View Slide

  104. Flowable Observable Maybe Single Completable
    Flowable toObservable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Observable toFlowable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Maybe toFlowable() toObservable()
    toSingle()
    sequenceEqual()
    toCompletable()
    Single toFlowable() toObservable() toMaybe() toCompletable()
    Completable toFlowable() toObservable() toMaybe()
    toSingle()
    toSingleDefault()
    From
    To

    View Slide

  105. Flowable Observable Maybe Single Completable
    Flowable toObservable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Observable toFlowable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Maybe toFlowable() toObservable()
    toSingle()
    sequenceEqual()
    toCompletable()
    Single toFlowable() toObservable() toMaybe() toCompletable()
    Completable toFlowable() toObservable() toMaybe()
    toSingle()
    toSingleDefault()
    From
    To

    View Slide

  106. Flowable Observable Maybe Single Completable
    Flowable toObservable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Observable toFlowable()
    reduce()
    elementAt()
    firstElement()
    lastElement()
    singleElement()
    scan()
    elementAt()
    first()/firstOrError()
    last()/lastOrError()
    single/singleOrError()
    all()/any()/count()
    (and more)
    ignoreElements()
    Maybe toFlowable() toObservable()
    toSingle()
    sequenceEqual()
    toCompletable()
    Single toFlowable() toObservable() toMaybe() toCompletable()
    Completable toFlowable() toObservable() toMaybe()
    toSingle()
    toSingleDefault()
    From
    To

    View Slide

  107. Creating Sources

    View Slide

  108. Creating Sources
    Flowable.just("Hello");

    Flowable.just("Hello", "World");

    Observable.just("Hello");

    Observable.just("Hello", "World");

    Maybe.just("Hello");

    Single.just("Hello");

    View Slide

  109. Creating Sources
    String[] array = { "Hello", "World" };

    List list = Arrays.asList(array);

    Flowable.fromArray(array);
    Flowable.fromIterable(list);
    Observable.fromArray(array);
    Observable.fromIterable(list);

    View Slide

  110. Creating Sources
    Future future = CompletableFuture.completedFuture("Hello");

    Flowable.fromFuture(future);

    Observable.fromFuture(future);

    View Slide

  111. Creating Sources
    Flowable.empty();


    Observable.empty();


    Maybe.empty();


    Completable.complete();

    View Slide

  112. Creating Sources
    Flowable.never();


    Observable.never();


    Maybe.never();
    Single.never();


    Completable.never();

    View Slide

  113. Creating Sources
    Flowable.error(new RuntimeException());

    Observable.error(new RuntimeException());

    Maybe.error(new RuntimeException());

    Single.error(new RuntimeException());

    Completable.error(new RuntimeException());

    View Slide

  114. Creating Sources
    Observable.fromCallable(new Callable() {

    @Override public String call() {Y

    return getName();

    }X

    });

    View Slide

  115. Creating Sources
    Observable.fromCallable(new Callable() {

    @Override public String call() throws Exception {Y

    return getName();Z

    }X

    });

    View Slide

  116. Creating Sources
    OkHttpClient client = // …
    Request request = // …
    Observable.fromCallable(new Callable() {

    @Override public String call() throws Exception {Y

    return client.newCall(request).execute();Z

    }X

    });


    getName()

    View Slide

  117. Creating Sources
    Flowable.fromCallable(() -> "Hello");


    Observable.fromCallable(() -> "Hello");


    Maybe.fromCallable(() -> "Hello");

    Maybe.fromAction(() -> System.out.println("Hello"));

    Maybe.fromRunnable(() -> System.out.println("Hello"))


    Single.fromCallable(() -> "Hello");


    Completable.fromCallable(() -> "Ignored!");

    Completable.fromAction(() -> System.out.println("Hello"));

    Completable.fromRunnable(() -> System.out.println("Hello"));

    View Slide

  118. Creating Sources
    Observable.create();

    View Slide

  119. Creating Sources
    Observable.create();
    100% LESS
    AWFUL!!!

    View Slide

  120. Creating Sources
    Observable.create(new ObservableOnSubscribe() {

    @Override

    public void subscribe(ObservableEmitter e) throws Exception {

    e.onNext("Hello");

    e.onComplete();

    }X

    });

    View Slide

  121. Creating Sources
    Observable.create(new ObservableOnSubscribe() {

    @Override

    public void subscribe(ObservableEmitter e) throws Exception {

    e.onNext("Hello");

    e.onComplete();

    }X

    });

    View Slide

  122. Creating Sources
    Observable.create(new ObservableOnSubscribe() {

    @Override

    public void subscribe(ObservableEmitter e) throws Exception {

    e.onNext("Hello");

    e.onComplete();

    }X

    });

    View Slide

  123. Creating Sources
    Observable.create(e -> {

    e.onNext("Hello");

    e.onComplete();

    });
    new ObservableOnSubscribe()

    @Override

    public void subscribe(ObservableEmitter ) throws Exception {



    }X


    View Slide

  124. Creating Sources
    Observable.create(e -> {

    e.onNext("Hello");
    e.onNext("World");

    e.onComplete();

    });

    View Slide

  125. Creating Sources
    OkHttpClient client = // …
    Request request = // …
    Observable.create(e -> {
    client.newCall(request).enqueue(new Callback() {
    @Override public void onResponse(Response r) throws IOException {
    e.onNext(r.body().string());
    e.onComplete();
    }A
    @Override public void onFailure(IOException e) {
    e.onError(e);
    }B
    });

    });

    e.onNext("Hello");
    e.onNext("World");

    e.onComplete();

    View Slide

  126. Creating Sources
    OkHttpClient client = // …
    Request request = // …
    Observable.create(e -> {
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
    @Override public void onResponse(Response r) throws IOException {
    e.onNext(r.body().string());
    e.onComplete();
    }A
    @Override public void onFailure(IOException e) {
    e.onError(e);
    }B
    });

    });

    View Slide

  127. Creating Sources
    OkHttpClient client = // …
    Request request = // …
    Observable.create(e -> {
    Call call = client.newCall(request);
    e.setCancelation(() -> call.cancel());
    call.enqueue(new Callback() {
    @Override public void onResponse(Response r) throws IOException {
    e.onNext(r.body().string());
    e.onComplete();
    }A
    @Override public void onFailure(IOException e) {
    e.onError(e);
    }B
    });

    });

    View Slide

  128. Creating Sources
    OkHttpClient client = // …
    Request request = // …
    Observable.create(e -> {
    Call call = client.newCall(request);
    e.setCancelation(() -> call.cancel());
    call.enqueue(new Callback() {
    @Override public void onResponse(Response r) throws IOException {
    e.onNext(r.body().string());
    e.onComplete();
    }A
    @Override public void onFailure(IOException e) {
    e.onError(e);
    }B
    });

    });

    View Slide

  129. Creating Sources
    View view = // …
    Observable.create(e -> {
    e.setCancelation(() -> view.setOnClickListener(null));
    view.setOnClickListener(v -> e.onNext(v));

    });
    OkHttpClient client = // ...
    Request request = // ...
    Call call = client.newCall(request);
    e.setCancelation(() -> call.cancel());
    call.enqueue(new Callback() {
    @Override public void onResponse(Response r) throws IOException {
    e.onNext(r.body().string());
    e.onComplete();
    }A
    @Override public void onFailure(IOException e) {
    e.onError(e);
    }B
    });


    View Slide

  130. Creating Sources
    Flowable.create(e -> { … });
    Observable.create(e -> { … });
    Maybe.create(e -> { … });
    Single.create(e -> { … });
    Completable.create(e -> { … });

    View Slide

  131. Observing Sources

    View Slide

  132. Observing Sources
    Observable Flowable
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Observer {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Disposable d);

    }B

    View Slide

  133. Observing Sources
    Observable Flowable
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Observer {

    void onNext(T t);A

    void onComplete();B

    void onError(Throwable t);C

    void onSubscribe(Disposable d);D

    }B
    interface Disposable {

    void dispose();

    }B
    interface Subscription {

    void cancel();

    void request(long r);

    }B

    View Slide

  134. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribe(new Observer() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    @Override public void onSubscribe(Disposable d) {
    ???
    }B

    });
    Flowable
    interface Subscriber {

    void onNext(T t);

    void onComplete();

    void onError(Throwable t);

    void onSubscribe(Subscription s);

    }B
    interface Disposable {

    void dispose();

    }B
    interface Subscription {

    void cancel();

    void request(long r);

    }B
    interface T

    T t ;A

    ;B

    ;C

    ;D


    View Slide

  135. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribe(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });




    @Override public void onSubscribe(Disposable d) {
    ???
    }B


    View Slide

  136. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribe(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z
    unsubscribe???

    View Slide

  137. Observing Sources
    Observable o = Observable.just("Hello");
    DisposableObserver observer = new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    }
    o.subscribe(observer);Z
    unsubscribe???

    View Slide

  138. Observing Sources
    Observable o = Observable.just("Hello");
    DisposableObserver observer = new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    }
    o.subscribe(observer);Z
    observer.dispose();

    View Slide

  139. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribe(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z

    View Slide

  140. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribeWith(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z

    View Slide

  141. Observing Sources
    Observable o = Observable.just("Hello");
    Disposable d = o.subscribeWith(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z
    d.dispose();

    View Slide

  142. Observing Sources
    Observable o = Observable.just("Hello");
    CompositeDisposable disposables = new CompositeDisposable();
    Disposable d = o.subscribeWith(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z
    disposables.add(d);




    d.dispose();

    View Slide

  143. Observing Sources
    Observable o = Observable.just("Hello");
    CompositeDisposable disposables = new CompositeDisposable();
    Disposable d = o.subscribeWith(new DisposableObserver() {

    @Override public void onNext(String s) { … }

    @Override public void onComplete() { … }

    @Override public void onError(Throwable t) { … }

    });Z
    disposables.add(d);
    disposables.dispose();

    View Slide

  144. Observing Sources
    Observable o = Observable.just("Hello");
    o.subscribeWith(new DisposableObserver() { … });Z
    Maybe m = Maybe.just("Hello");
    m.subscribeWith(new DisposableMaybeObserver() { … });Z
    Single s = Single.just("Hello");
    s.subscribeWith(new DisposableSingleObserver() { … });Z
    Completable c = Completable.completed();
    c.subscribeWith(new DisposableCompletableObserver() { … });Z

    View Slide

  145. Observing Sources
    Flowable f = Flowable.just("Hello");
    f.subscribeWith(new DisposableSubscriber() { … });
    Observable o = Observable.just("Hello");
    o.subscribeWith(new DisposableObserver() { … });
    Maybe m = Maybe.just("Hello");
    m.subscribeWith(new DisposableMaybeObserver() { … });
    Single s = Single.just("Hello");
    s.subscribeWith(new DisposableSingleObserver() { … });
    Completable c = Completable.completed();
    c.subscribeWith(new DisposableCompletableObserver() { … });

    View Slide

  146. Observing Sources
    Flowable f = Flowable.just("Hello");
    f.subscribeWith(new DisposableSubscriber() { … });
    Observable o = Observable.just("Hello");
    o.subscribeWith(new DisposableObserver() { … });
    Maybe m = Maybe.just("Hello");
    m.subscribeWith(new DisposableMaybeObserver() { … });
    Single s = Single.just("Hello");
    s.subscribeWith(new DisposableSingleObserver() { … });
    Completable c = Completable.completed();
    c.subscribeWith(new DisposableCompletableObserver() { … });

    View Slide

  147. Observing Sources
    Flowable f = Flowable.just("Hello");
    Disposable d1 = f.subscribeWith(new DisposableSubscriber() { … });
    Observable o = Observable.just("Hello");
    Disposable d2 = o.subscribeWith(new DisposableObserver() { … });
    Maybe m = Maybe.just("Hello");
    Disposable d3 = m.subscribeWith(new DisposableMaybeObserver() { … });
    Single s = Single.just("Hello");
    Disposable d4 = s.subscribeWith(new DisposableSingleObserver() { … });
    Completable c = Completable.completed();
    Disposable d5 = c.subscribeWith(new DisposableCompletableObserver() { … });

    View Slide

  148. RxJava

    View Slide

  149. RxJava
    • 2.0.0-RC3 was released 2016-09-23.

    View Slide

  150. RxJava
    • 2.0.0-RC3 was released 2016-09-23.
    • Releases are more like developer previews than release candidates.

    View Slide

  151. RxJava
    • 2.0.0-RC3 was released 2016-09-23.
    • Releases are more like developer previews than release candidates.
    • Next two are scheduled for 2016-10-07 and 2016-10-21.

    View Slide

  152. RxJava
    • 2.0.0-RC3 was released 2016-09-23.
    • Releases are more like developer previews than release candidates.
    • Next two are scheduled for 2016-10-07 and 2016-10-21.
    • Final release scheduled for 2016-10-29.

    View Slide

  153. RxJava
    • Package name is io.reactivex.*.

    View Slide

  154. RxJava
    • Package name is io.reactivex.*.
    • Maven coordinates are io.reactivex.rxjava2:rxjava.

    View Slide

  155. RxJava
    • Package name is io.reactivex.*.
    • Maven coordinates are io.reactivex.rxjava2:rxjava.
    • Why the change? See http://jakes.link/major-versions

    View Slide

  156. RxAndroid

    View Slide

  157. RxAndroid
    • 2.0.0-RC1 was released 2016-08-25.

    View Slide

  158. RxAndroid
    • 2.0.0-RC1 was released 2016-08-25.
    • Provides facilities for dealing with the main thread (or any Looper).

    View Slide

  159. RxAndroid
    • 2.0.0-RC1 was released 2016-08-25.
    • Provides facilities for dealing with the main thread (or any Looper).
    • Package name is io.reactivex.android.*.

    View Slide

  160. RxAndroid
    • 2.0.0-RC1 was released 2016-08-25.
    • Provides facilities for dealing with the main thread (or any Looper).
    • Package name is io.reactivex.android.*.
    • Maven coordinates are io.reactivex.rxjava2:rxandroid.

    View Slide

  161. Living with 1.x and 2.x

    View Slide

  162. Living with 1.x and 2.x
    class RxJavaInterop {

    static Flowable toV2Flowable(rx.Observable o) { … }

    static Observable toV2Observable(rx.Observable o) { … }
    static Maybe toV2Maybe(rx.Single s) { … }
    static Maybe toV2Maybe(rx.Completable c) { … }

    static Single toV2Single(rx.Single s) { … }

    static Completable toV2Completable(rx.Completable c) { … }

    static rx.Observable toV1Observable(Publisher p) { … }
    static rx.Observable toV1Observable(Observable o, …) { … }

    static rx.Single toV1Single(Single o) { … }
    static rx.Single toV1Single(Maybe m) { … }

    static rx.Completable toV1Completable(Completable c) { … }
    static rx.Completable toV1Completable(Maybe m) { … }

    }X

    View Slide

  163. Living with 1.x and 2.x
    class RxJavaInterop {

    static Flowable toV2Flowable(rx.Observable o) { … }

    static Observable toV2Observable(rx.Observable o) { … }
    static Maybe toV2Maybe(rx.Single s) { … }
    static Maybe toV2Maybe(rx.Completable c) { … }

    static Single toV2Single(rx.Single s) { … }

    static Completable toV2Completable(rx.Completable c) { … }

    static rx.Observable toV1Observable(Publisher p) { … }
    static rx.Observable toV1Observable(Observable o, …) { … }

    static rx.Single toV1Single(Single o) { … }
    github.com/akarnokd/RxJava2Interop

    View Slide

  164. dependencies {

    compile 'io.reactivex.rxjava2:rxjava:2.0.0-RC3'

    compile 'io.reactivex.rxjava2:rxandroid:2.0.0-RC1'
    // Optionally...

    compile 'com.github.akarnokd:rxjava2-interop:0.3.0'
    }

    View Slide

  165. Other Libraries We Control

    View Slide

  166. Other Libraries We Control
    • Retrofit 2 – github.com/JakeWharton/retrofit2-rxjava2-adapter/

    View Slide

  167. Other Libraries We Control
    • Retrofit 2 – github.com/JakeWharton/retrofit2-rxjava2-adapter/
    • RxBinding – waiting for 2.0 final
    • RxRelay – waiting for 2.0 final
    • RxReplayingShare – waiting for 2.0 final
    • SQL Brite – waiting for 2.0 final
    • Whorlwind – waiting for 2.0 final

    View Slide

  168. RxJava 2

    View Slide

  169. RxJava 2
    • Reactive Streams compliant.

    View Slide

  170. RxJava 2
    • Reactive Streams compliant.
    • Backpressure in the type system (Flowable).

    View Slide

  171. RxJava 2
    • Reactive Streams compliant.
    • Backpressure in the type system (Flowable).
    • New Maybe type.

    View Slide

  172. RxJava 2
    • Reactive Streams compliant.
    • Backpressure in the type system (Flowable).
    • New Maybe type.
    • Actually usable create().

    View Slide

  173. RxJava 2
    • Reactive Streams compliant.
    • Backpressure in the type system (Flowable).
    • New Maybe type.
    • Actually usable create().
    • All the same operators.

    View Slide

  174. RxJava 2
    • Reactive Streams compliant.
    • Backpressure in the type system (Flowable).
    • New Maybe type.
    • Actually usable create().
    • All the same operators.
    • Preview releases now, final release in a month.

    View Slide

  175. jakewharton
    jakewharton
    jakewharton
    twitter.com/
    google.com/+
    .com
    Looking Ahead To

    RxJava 2

    View Slide