Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can...

211

Transcript of Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can...

Page 1: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire
Page 2: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Exploring RxJava

Jake Wharton

Page 3: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Exploring RxJava 2

Jake Wharton

Page 4: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Unless you can model your entire system synchronously...

Page 5: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Unless you can model your entire system synchronously, a single asynchronous source breaks

imperative programming.

Page 6: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); }A

Page 7: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

Page 8: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

UserManager um = new UserManager();

Page 9: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

UserManager um = new UserManager(); System.out.println(um.getUser());

Page 10: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe");

Page 11: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe"); System.out.println(um.getUser());

Page 12: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

Page 13: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); // <-- now async void setAge(int age); // <-- now async}A

Page 14: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe"); System.out.println(um.getUser());

Page 15: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

Page 16: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name, Runnable callback); void setAge(int age, Runnable callback); }A

Page 17: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name, Runnable callback);A void setAge(int age, Runnable callback);B }A

UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe", new Runnable() { @Override public void run() { System.out.println(um.getUser()); }X });

Page 18: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name);A void setAge(int age);B }A

Page 19: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?interface UserManager { User getUser(); void setName(String name, Listener listener);A void setAge(int age, Listener listener);B

interface Listener { void success(User user); void failure(IOException e); }G }A

Page 20: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe");

Page 21: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser()); }A @Override public void failure(IOException e) { // TODO show the error... }B });

Page 22: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser()); }A @Override public void failure(IOException e) { // TODO show the error... }B });

um.setAge(40, new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser()); }C 2@Override public void failure(IOException e) {2 2// TODO show the error...2 }D });2

Page 23: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?UserManager um = new UserManager(); System.out.println(um.getUser());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser());

um.setAge(40, new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser()); }C 2@Override public void failure(IOException e) {2 2// TODO show the error...2 }D });2 }A @Override public void failure(IOException e) { // TODO show the error... }B });

Page 24: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { tv.setText(um.getUser().toString()); }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

System.out.println(um.getUser());

System.out.println(um.getUser());

um.setAge(40, new UserManager.Listener() { @Override public void success() { System.out.println(um.getUser()); }C 2@Override public void failure(IOException e) {2 2// TODO show the error...2 }D });2

Page 25: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { tv.setText(um.getUser().toString()); }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 26: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { if (isDestroyed()) { tv.setText(um.getUser().toString()); } }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 27: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { if (isDestroyed()) { tv.setText(um.getUser().toString()); } }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 28: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { if (isDestroyed()) { tv.setText(um.getUser().toString()); }L }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 29: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { runOnUiThread(new Runnable() { @Override public void run() { if (isDestroyed()) { tv.setText(um.getUser().toString()); }L }4 }); }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 30: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?public final class UserActivity extends Activity { private final UserManager um = new UserManager();

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.user); TextView tv = (TextView) findViewById(R.id.user_name); tv.setText(um.getUser().toString());

um.setName("Jane Doe", new UserManager.Listener() { @Override public void success() { runOnUiThread(new Runnable() { @Override public void run() { if (isDestroyed()) { tv.setText(um.getUser().toString()); } }4 }); }A @Override public void failure(IOException e) { // TODO show the error... }B }); }Y }Z

Page 31: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

GET /

200 OKSELECT *

Jane DoesetText

onClick

Page 32: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

GET /

200 OKSELECT *

Jane Doe setText

onClick

SELECT *

Jane Doe setText

UPDATE user

Jane Doe setText

Page 33: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

GET /

200 OKSELECT *

Jane Doe setText

onClick

SELECT *

Jane Doe setText

UPDATE user

Jane Doe setText

Page 34: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 35: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Unless you can model your entire system synchronously, a single asynchronous source breaks

imperative programming.

Page 36: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 37: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 38: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 39: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 40: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Why Reactive?

Page 41: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

RxJava• A set of classes for representing sources of data.

• A set of classes for listening to data sources.

• A set of methods for modifying and composing the data.

Page 42: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

RxJava• A set of classes for representing sources of data.

• A set of classes for listening to data sources.

• A set of methods for modifying and composing the data.

Page 43: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources

Page 44: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

Page 45: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

Page 46: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

• Single item or many items.

Page 47: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

• Single item, many items, or empty.

Page 48: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

• Single item, many items, or empty.

• Terminates with an error or succeeds to completion.

Page 49: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

• Single item, many items, or empty.

• Terminates with an error or succeeds to completion.

• May never terminate!

Page 50: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sources• Usually do work when you start or stop listening.

• Synchronous or asynchronous.

• Single item, many items, or empty.

• Terminates with an error or succeeds to completion.

• May never terminate!

• Just an implementation of the Observer pattern.

Page 51: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

• Observable<T>

• Flowable<T> void void

Sources

Page 52: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

• Observable<T>

• Emits 0 to n items.

• Terminates with complete or error.

• Flowable<T>

• Emits 0 to n items.

• Terminates with complete or error.

void void

Sources

Page 53: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

• Observable<T>

• Emits 0 to n items.

• Terminates with complete or error.

• Does not have backpressure.

• Flowable<T>

• Emits 0 to n items.

• Terminates with complete or error.

• Has backpressure.

void void

Sources

Page 54: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure allows you to control how fast a source emits items.

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

Page 55: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure, like inheritance, must be designed for.

Page 56: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure, like inheritance, must be designed for.

Observable<MotionEvent> events = RxView.touches(paintView);

Page 57: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure, like inheritance, must be designed for.

Observable<MotionEvent> events = RxView.touches(paintView);

Observable<Row> rows = db.createQuery("SELECT * …");

Page 58: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure, like inheritance, must be designed for.

Observable<MotionEvent> events = RxView.touches(paintView);

Observable<Row> rows = db.createQuery("SELECT * …");

MissingBackpressureException

Page 59: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. Observable• Backpressure, like inheritance, must be designed for.

Observable<MotionEvent> events = RxView.touches(paintView);

Flowable<Row> rows = db.createQuery("SELECT * …");

Page 60: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. ObservableObservable<MotionEvent> Flowable<Row>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

interface Observer<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Disposable d); }B

Page 61: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. ObservableObservable<MotionEvent> Flowable<Row>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s);}B

interface Observer<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Disposable d);}B

Page 62: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. ObservableObservable<MotionEvent> Flowable<Row>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s);}B

interface Observer<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Disposable d);}B

Page 63: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. ObservableObservable<MotionEvent> Flowable<Row>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s);}B

interface Observer<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Disposable d);}B

Page 64: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Flowable vs. ObservableObservable<MotionEvent> Flowable<Row>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

interface Observer<T> { 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

Page 65: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Backpressure No Backpressure

0…n items, complete|error Flowable Observable

Page 66: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streams

...is an initiative to provide a standard for asynchronous stream processing

with non-blocking back pressure.

Page 67: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streamsinterface Publisher<T> { void subscribe(Subscriber<? super T> s); }A

Page 68: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streamsinterface Publisher<T> { void subscribe(Subscriber<? super T> s); }A

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

Page 69: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streamsinterface Publisher<T> { void subscribe(Subscriber<? super T> s); }A

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

interface Subscription { void request(long n); void cancel(); }C

Page 70: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streamsinterface Publisher<T> { void subscribe(Subscriber<? super T> s); }A

interface Subscriber<T> { 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<T, R> extends Subscriber<T>, Publisher<R> {}D

Page 71: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Backpressure No Backpressure

0…n items, complete|error Flowable Observable

Page 72: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streams(Backpressure) No Backpressure

0…n items, complete|error Flowable Observable

Page 73: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

interface UserManager { User getUser(); void setName(String name); void setAge(int age); }A

Sources

Page 74: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sourcesinterface UserManager { Observable<User> getUser(); void setName(String name); void setAge(int age); }A

Page 75: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

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

Page 76: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Single• Either succeeds with an item or errors.

• No backpressure support.

Page 77: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Single• Either succeeds with an item or errors.

• No backpressure support.

• Think "reactive scalar".

Page 78: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Completable• Either completes or errors. Has no items!

• No backpressure support.

Page 79: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Completable• Either completes or errors. Has no items!

• No backpressure support.

• Think "reactive runnable".

Page 80: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Maybe• Either succeeds with an item, completes with no items, or errors.

• No backpressure support.

Page 81: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Maybe• Either succeeds with an item, completes with no items, or errors.

• No backpressure support.

• Think "reactive optional".

Page 82: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Source 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".

Page 83: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Reactive Streams(Backpressure) No Backpressure

0…n items, complete|error Flowable Observable

item|complete|error Maybe

item|error Single

complete|error Completable

Page 84: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sourcesinterface UserManager { Observable<User> getUser(); void setName(String name); void setAge(int age); }A

Page 85: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Sourcesinterface UserManager { Observable<User> getUser(); Completable setName(String name); Completable setAge(int age); }A

void void

Page 86: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating Sources

Page 87: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesFlowable.just("Hello"); Flowable.just("Hello", "World"); Observable.just("Hello"); Observable.just("Hello", "World");

Maybe.just("Hello");

Single.just("Hello");

Page 88: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesString[] array = { "Hello", "World" }; List<String> list = Arrays.asList(array);

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

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

Page 89: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.fromCallable(new Callable<String>() { @Override public String call() {Y return getName(); }X });

Page 90: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.fromCallable(new Callable<String>() { @Override public String call() throws Exception {Y return getName();Z }X });

Page 91: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesOkHttpClient client = // … Request request = // …

Observable.fromCallable(new Callable<String>() { @Override public String call() throws Exception {Y return client.newCall(request).execute();Z }X });

getName()

Page 92: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesFlowable.fromCallable(() -> "Hello"); Observable.fromCallable(() -> "Hello"); Maybe.fromCallable(() -> "Hello"); Single.fromCallable(() -> "Hello"); Completable.fromCallable(() -> "Ignored!");

Page 93: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesFlowable.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"));

Page 94: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create();

Page 95: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { e.onNext("Hello"); e.onComplete(); }X });

Page 96: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { e.onNext("Hello"); e.onComplete(); }X });

Page 97: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { e.onNext("Hello"); e.onComplete(); }X });

Page 98: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { e.onNext("Hello"); e.onComplete(); }X });

Page 99: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(e -> { e.onNext("Hello"); e.onComplete(); });

new ObservableOnSubscribe<String>() @Override public void subscribe(ObservableEmitter<String> ) throws Exception { }X

Page 100: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesObservable.create(e -> { e.onNext("Hello"); e.onNext("World"); e.onComplete(); });

Page 101: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesOkHttpClient 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();

Page 102: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesOkHttpClient 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 }); });

Page 103: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesOkHttpClient 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 }); });

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

Page 104: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesView 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 });

Page 105: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Creating SourcesFlowable.create(e -> { … });

Observable.create(e -> { … });

Maybe.create(e -> { … });

Single.create(e -> { … });

Completable.create(e -> { … });

Page 106: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing Sources

Page 107: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> Flowable<String>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

interface Observer<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Disposable d); }B

Page 108: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> Flowable<String>

interface Subscriber<T> { void onNext(T t); void onComplete(); void onError(Throwable t); void onSubscribe(Subscription s); }B

interface Observer<T> { 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

Page 109: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

o.subscribe(new Observer<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … } @Override public void onSubscribe(Disposable d) { ??? }B });

Flowable<String>

interface Subscriber<T> { 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

Page 110: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

o.subscribe(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }});

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

Page 111: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

o.subscribe(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }});Z

// TODO how do we dispose???

Page 112: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

DisposableObserver observer = new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }} o.subscribe(observer);Z// TODO how do we dispose??????

Page 113: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

DisposableObserver observer = new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }} o.subscribe(observer);Z

observer.dispose();

Page 114: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

o.subscribe(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }});Z

Page 115: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

o.subscribeWith(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }});Z

Page 116: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

Disposable d = o.subscribeWith(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }});Z

d.dispose();

Page 117: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello");

CompositeDisposable disposables = new CompositeDisposable();

disposables.add(o.subscribeWith(new DisposableObserver<String>() { @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … }}));Z

disposables.dispose();

d.dispose();

Disposable d =

Page 118: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesObservable<String> o = Observable.just("Hello"); o.subscribeWith(new DisposableObserver<String>() { … });Z

Maybe<String> m = Maybe.just("Hello"); m.subscribeWith(new DisposableMaybeObserver<String>() { … });Z

Single<String> s = Single.just("Hello"); s.subscribeWith(new DisposableSingleObserver<String>() { … });Z

Completable c = Completable.completed(); c.subscribeWith(new DisposableCompletableObserver<String>() { … });Z

disposables.add( @Override public void onNext(String s) { … } @Override public void onComplete() { … } @Override public void onError(Throwable t) { … } )

disposables.dispose();

Page 119: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesFlowable<String> f = Flowable.just("Hello"); f.subscribeWith(new DisposableSubscriber<String>() { … });

Observable<String> o = Observable.just("Hello"); o.subscribeWith(new DisposableObserver<String>() { … });

Maybe<String> m = Maybe.just("Hello"); m.subscribeWith(new DisposableMaybeObserver<String>() { … });

Single<String> s = Single.just("Hello"); s.subscribeWith(new DisposableSingleObserver<String>() { … });

Completable c = Completable.completed(); c.subscribeWith(new DisposableCompletableObserver<String>() { … });

Flowable<String> f = Flowable.just("Hello"); f.subscribeWith(new DisposableSubscriber<String>() { … });

Observable<String> o = Observable.just("Hello"); o.subscribeWith(new DisposableObserver<String>() { … });

Maybe<String> m = Maybe.just("Hello"); m.subscribeWith(new DisposableMaybeObserver<String>() { … });

Single<String> s = Single.just("Hello"); s.subscribeWith(new DisposableSingleObserver<String>() { … });

Completable c = Completable.completed(); c.subscribeWith(new DisposableCompletableObserver<String>() { … });

Page 120: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observing SourcesFlowable<String> f = Flowable.just("Hello"); Disposable d1 = f.subscribeWith(new DisposableSubscriber<String>() { … });

Observable<String> o = Observable.just("Hello"); Disposable d2 = o.subscribeWith(new DisposableObserver<String>() { … });

Maybe<String> m = Maybe.just("Hello"); Disposable d3 = m.subscribeWith(new DisposableMaybeObserver<String>() { … });

Single<String> s = Single.just("Hello"); Disposable d4 = s.subscribeWith(new DisposableSingleObserver<String>() { … });

Completable c = Completable.completed(); Disposable d5 = c.subscribeWith(new DisposableCompletableObserver<String>() { … });

Page 121: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

RxJava• A set of classes for representing sources of data.

• A set of classes for listening to data sources.

• A set of methods for modifying and composing data.

Page 122: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

RxJava• A set of classes for representing sources of data.

• A set of classes for listening to data sources.

• A set of methods for modifying and composing data.

Page 123: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operators• Manipulate or combine data in some way.

• Manipulate threading in some way.

• Manipulate emissions in some way.

Page 124: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsString greeting = "Hello";

Page 125: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsString greeting = "Hello";Z String yelling = greeting.toUppercase();

Page 126: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello");Z String yelling = greeting.toUppercase();Y

Page 127: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello"); Observable<String> yelling = greeting.map(s -> s.toUppercase());Y

Page 128: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello"); Observable<String> yelling = greeting.map(s -> s.toUppercase());Y

Page 129: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsString greeting = "Hello, World!";

Page 130: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsString greeting = "Hello, World!";Z String[] words = greeting.split(" ");

Page 131: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello, World!");Z String[] words = greeting.split(" ");Y

Page 132: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello, World!");Z Observable<String[]> words = greeting.map(s -> s.split(" "));Y

Page 133: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello, World!");Z Observable<Observable<String>> words = greeting.map(s -> Observable.fromArray(s.split(" ")));Y

Page 134: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello, World!");Z Observable<String> words = greeting.flatMap(s -> Observable.fromArray(s.split(" ")));Y Observable< > map

Page 135: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<String> greeting = Observable.just("Hello, World!"); Observable<String> words = greeting.flatMap(s -> Observable.fromArray(s.split(" ")));

Page 136: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operators@Override public void success() { runOnUiThread(new Runnable() { @Override public void run() { tv.setText(um.getUser().toString()); }4 }); }A

Page 137: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<User> user = um.getUser();

Page 138: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<User> user = um.getUser(); Observable<User> mainThreadUser = user.observeOn(AndroidSchedulers.mainThread());

Page 139: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsObservable<User> user = um.getUser(); Observable<User> mainThreadUser = user.observeOn(AndroidSchedulers.mainThread());

Page 140: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Response response = client.newCall(request).execute();

Page 141: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); });

Page 142: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }); Observable<Response> backgroundResponse = response.subscribeOn(Schedulers.io());

Page 143: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }); Observable<Response> backgroundResponse = response.subscribeOn(Schedulers.io());

Page 144: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }); Observable<Response> backgroundResponse = response.subscribeOn(Schedulers.io());

Page 145: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io());Y

Page 146: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread());Y

Page 147: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map(response -> response.body().string());Y

Page 148: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map(response -> response.body().string());Y// NetworkOnMainThread!

Page 149: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io()) .map(response -> response.body().string()) // Ok! .observeOn(AndroidSchedulers.mainThread());Y// NetworkOnMainThread!

Page 150: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OperatorsOkHttpClient client = // … Request request = // …

Observable<Response> response = Observable.fromCallable(() -> { return client.newCall(request).execute(); }) .subscribeOn(Schedulers.io()) .map(response -> response.body().string()) // Ok! .flatMap(s -> Observable.fromArray(s.split(" "))) .observeOn(AndroidSchedulers.mainThread());Y// NetworkOnMainThread!

Page 151: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

Page 152: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

1 2 3 4 5 6 7 8 9 10 11

first()

1

Observable

Observable

Page 153: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

1 2 3 4 5 6 7 8 9 10 11

1

Page 154: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

1 2 3 4 5 6 7 8 9 10 11

get(0)

1

List<Long>

List<Long>

Page 155: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

Page 156: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

first()

Observable

Observable

Page 157: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

1 2 3 4 5 6 7 8 9 10 11

first()

1

Observable

Single

Page 158: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

first()

Observable

Single

NoSuchElementException

Page 159: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

firstElement()

Observable

Maybe

Page 160: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

1 2 3 4 5 6 7 8 9

ignoreElements()

Observable

Completable

Page 161: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

ignoreElements()

Flowable

Completable

Page 162: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

firstElement()

Flowable

Maybe

Page 163: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Operator Specialization

first()

Flowable

Single

Page 164: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

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()

FromTo

Page 165: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

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()

FromTo

Page 166: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

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()

FromTo

Page 167: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactive

Page 168: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.getUser()

Page 169: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.getUser() .observeOn(AndroidSchedulers.mainThread())

Page 170: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<User>() { @Override public void onNext(User user) { }1 @Override public void onComplete() { /* ignored */ } @Override public void onError(Throwable t) { /* crash or show */ } });

Page 171: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<User>() { @Override public void onNext(User user) { tv.setText(user.toString()); }1 @Override public void onComplete() { /* ignored */ } @Override public void onError(Throwable t) { /* crash or show */ } });2

Page 172: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactivedisposables.add(um.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<User>() { @Override public void onNext(User user) { tv.setText(user.toString()); }1 @Override public void onComplete() { /* ignored */ } @Override public void onError(Throwable t) { /* crash or show */ } }));2

Page 173: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactive// onCreate disposables.add(um.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<User>() { @Override public void onNext(User user) { tv.setText(user.toString()); }1 @Override public void onComplete() { /* ignored */ } @Override public void onError(Throwable t) { /* crash or show */ } }));

// onDestroy disposables.dispose();

Page 174: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.setName("Jane Doe")

Page 175: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.setName("Jane Doe") .subscribeOn(Schedulers.io())

Page 176: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.setName("Jane Doe") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())

Page 177: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.setName("Jane Doe") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableCompletableObserver() { @Override public void onComplete() { }1 @Override public void onError(Throwable t) { // retry or show }2 });

Page 178: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactiveum.setName("Jane Doe") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableCompletableObserver() { @Override public void onComplete() { // success! re-enable editing }1 @Override public void onError(Throwable t) { // retry or show }2 });3

Page 179: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Being Reactivedisposables.add(um.setName("Jane Doe") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableCompletableObserver() { @Override public void onComplete() { // success! re-enable editing }1 @Override public void onError(Throwable t) { // retry or show }2 }));3

Page 180: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture

Page 181: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (1.x)

Page 182: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (1.x)Observable.just("hello") "hello"

Page 183: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (1.x)Observable.just("hello") OnSubscribe<String>

"hello"

Page 184: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (1.x)Observable.just("hello") Observable<String>

OnSubscribe<String> "hello"

Page 185: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase()) s -> s.toUpperCase()

Page 186: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OnSubscribe<String> s -> s.toUpperCase()

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase()) OnSubscribe<String>

Func1<String, String>

Page 187: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase()) Observable<String>

OnSubscribe<String> Func1<String, String>

Page 188: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String>

OnSubscribe<String> Func1<String, String>

Page 189: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String>

OnSubscribe<String> Func1<String, String>

Page 190: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String>

OnSubscribe<String> Func1<String, String>

Subscriber<String> println,printStackTrace

Page 191: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String>

OnSubscribe<String> Func1<String, String>

Subscriber<String> println,printStackTrace

Page 192: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String>

Subscriber<String> println,printStackTrace

OnSubscribe<String> Func1<String, String>

Subscriber<String> Func1<String, String>

Page 193: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Observable<String>

Architecture (1.x)

OnSubscribe<String> "hello"

Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Subscriber<String> println,printStackTrace

Subscriber<String> Func1<String, String>

Observable<String>

OnSubscribe<String> Func1<String, String>

Page 194: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

ArchitectureArchitecture (2.x)

Page 195: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello") "hello"

Page 196: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

Observable<String> "hello"

Page 197: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase()) s -> s.toUpperCase()

Observable<String> "hello"

Page 198: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

OnSubscribe<String> s -> s.toUpperCase()

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

Observable<String> Func1<String, String>

Observable<String> "hello"

Page 199: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Page 200: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Page 201: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Observer<String> println,printStackTrace

Page 202: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Observer<String> println,printStackTrace

Page 203: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Observer<String> println,printStackTrace

Observer<String> Func1<String, String>

Page 204: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)Observable.just("hello")

.map(s -> s.toUpperCase())

.subscribe( System.out::println, Throwable::printStackTrace);

Observable<String> Func1<String, String>

Observable<String> "hello"

Observer<String> println,printStackTrace

Observer<String> Func1<String, String>

Page 205: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Architecture (2.x)• Less allocation to create a stream.

• Less overhead when subscribing to a stream.

Page 206: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

RxJava 2• Build an app that reacts properly instead of copes desperately!

• Final release scheduled for 2016-10-29.

Page 207: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Living with 1.x and 2.xclass RxJavaInterop { static <T> Flowable<T> toV2Flowable(rx.Observable<T> o) { … } static <T> Observable<T> toV2Observable(rx.Observable<T> o) { … } static <T> Maybe<T> toV2Maybe(rx.Single<T> s) { … } static <T> Maybe<T> toV2Maybe(rx.Completable c) { … } static <T> Single<T> toV2Single(rx.Single<T> s) { … } static Completable toV2Completable(rx.Completable c) { … } static <T> rx.Observable<T> toV1Observable(Publisher<T> p) { … } static <T> rx.Observable<T> toV1Observable(Observable<T> o, …) { … } static <T> rx.Single<T> toV1Single(Single<T> o) { … } static <T> rx.Single<T> toV1Single(Maybe<T> m) { … } static rx.Completable toV1Completable(Completable c) { … } static rx.Completable toV1Completable(Maybe<T> m) { … } }X

Page 208: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

Living with 1.x and 2.x

class RxJavaInterop { static <T> Flowable<T> toV2Flowable(rx.Observable<T> o) { … } static <T> Observable<T> toV2Observable(rx.Observable<T> o) { … } static <T> Maybe<T> toV2Maybe(rx.Single<T> s) { … } static <T> Maybe<T> toV2Maybe(rx.Completable c) { … } static <T> Single<T> toV2Single(rx.Single<T> s) { … } static Completable toV2Completable(rx.Completable c) { … } static <T> rx.Observable<T> toV1Observable(Publisher<T> p) { … } static <T> rx.Observable<T> toV1Observable(Observable<T> o, …) { … } static <T> rx.Single<T> toV1Single(Single<T> o) { … } static <T> rx.Single<T> toV1Single(Maybe<T> m) { … }

github.com/akarnokd/RxJava2Interop

Page 209: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

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' }

Page 210: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire

jakewhartonjakewharton

jakewharton

twitter.com/

google.com/+

.com

Exploring RxJava

Page 211: Exploring RxJava - GOTO ConferenceExploring RxJava 2 Jake Wharton. Why Reactive? Unless you can model your entire system synchronously... Why Reactive? Unless you can model your entire