Slide 1

Slide 1 text

RxJava 2.0 介紹 黃千碩 (Kros) oSolve, Ltd./打⼯工趣 Mobile App Developer

Slide 2

Slide 2 text

• 如果尚未了了解 RxJava,可以看 • RxJava 介紹與 Android 中的 RxJava
 https://speakerdeck.com/ch8908/rxjava-jie-shao-yu-android- zhong-de-rxjava

Slide 3

Slide 3 text

為什什麼要有 2.0 • RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. The specification itself has evolved out of RxJava 1.x and provides a common baseline for reactive systems and libraries. • 因為要符合 Reactive-Streams specification。所以把 RxJava Library 重寫⼀一遍,稱作 2.0

Slide 4

Slide 4 text

什什麼是Reactive Streams • Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols. • 簡單來來說就是⼀一個 “處理理 asynchronous 程式” 的標準規範

Slide 5

Slide 5 text

反觀 Java 9 • Java 9 也⽀支援 Reactive-Streams specification • Reactive Programming with JDK 9 Flow API
 https://community.oracle.com/docs/DOC-1006738

Slide 6

Slide 6 text

interface Publisher {
 void subscribe(Subscriber super T> s);
 }
 
 interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void request(long n);
 void cancel();
 } interface Processor extends Subscriber, Publisher { } Reactive-Streams specification

Slide 7

Slide 7 text

Reactive Programming 是趨勢

Slide 8

Slide 8 text

Reactive Programming 是趨勢 直接從 RxJava 2.0 開始學吧!

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

Sources • 通常當你開始(或結束)監聽時,才開始運作。 • 運作時可以是 synchronous 或 asynchronous。 • Source 傳送的資料可以是 Single item, many items, 或 empty。 • 當發⽣生 error 或收到 complete 時結束。也或者永遠不結束。

Slide 12

Slide 12 text

2.0 差別 • 新增了了不同的 來來源(Source) • RxJava 1.0 - Only Observable • RxJava 2.0 - Observable And Flowable

Slide 13

Slide 13 text

interface Publisher {
 void subscribe(Subscriber super T> s);
 }
 
 interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void request(long n);
 void cancel();
 } interface Processor extends Subscriber, Publisher { } Flowable vs. Observable

Slide 14

Slide 14 text

interface Publisher {
 public void subscribe(Subscriber super T> s);
 } Flowable Flowable vs. Observable Observable interface ObservableSource {
 void subscribe(Observer super T> observer);
 }

Slide 15

Slide 15 text

Flowable vs. Observable • Observable • 發射 0 ~ n 筆資料 • 收到 complete 或 error 時結束 • 沒有流量量控制(backpressure) • Flowable • 發射 0 ~ n 筆資料 • 收到 complete 或 error 時結束 • 有流量量控制(backpressure)

Slide 16

Slide 16 text

Flowable vs. Observable • Observable • 發射 0 ~ n 筆資料 • 收到 complete 或 error 時結束 • 沒有流量量控制 • Flowable • 發射 0 ~ n 筆資料 • 收到 complete 或 error 時結束 • 有流量量控制

Slide 17

Slide 17 text

Reactive-Streams specification interface Publisher {
 void subscribe(Subscriber super T> s);
 }
 
 interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void request(long n);
 void cancel();
 } interface Processor extends Subscriber, Publisher { } 流量量控制 (Backpressure)

Slide 18

Slide 18 text

Flowable vs. Observable • Why Flowable and Observable? • 為什什麼不統⼀一使⽤用⼀一種就好?

Slide 19

Slide 19 text

Flowable vs. Observable • 本質上,不是每⼀一種 Source 都能夠流量量控制(Backpressure)。

Slide 20

Slide 20 text

Flowable vs. Observable Observable events = RxView.touches(paintView)


Slide 21

Slide 21 text

Flowable vs. Observable Observable events = RxView.touches(paintView)
 無法控制 User 畫圖的事件, 本質上無法做流量量控制(Backpressure)

Slide 22

Slide 22 text

Flowable vs. Observable Observable events = RxView.touches(paintView)
 Observable rows = db.createQuery("SELECT *")
 無法控制 User 畫圖的事件, 本質上無法做流量量控制(Backpressure)

Slide 23

Slide 23 text

Flowable vs. Observable Observable events = RxView.touches(paintView)
 Observable rows = db.createQuery("SELECT *")
 while (cursor.moveToNext()) { // … }

Slide 24

Slide 24 text

Flowable vs. Observable Observable events = RxView.touches(paintView)
 Observable rows = db.createQuery("SELECT *")
 while (cursor.moveToNext()) { // … } Cursor 就是流量量控制

Slide 25

Slide 25 text

Flowable vs. Observable Observable events = RxView.touches(paintView)
 Flowable rows = db.createQuery("SELECT *")
 while (cursor.moveToNext()) { // … } • 要針對不同的情況,使⽤用不同的 Design Pattern。

Slide 26

Slide 26 text

interface Publisher {
 public void subscribe(Subscriber super T> s);
 } Flowable Flowable vs. Observable Observable interface ObservableSource {
 void subscribe(Observer super T> observer);
 }

Slide 27

Slide 27 text

interface Publisher {
 public void subscribe(Subscriber super T> s);
 } Flowable Flowable vs. Observable Observable interface ObservableSource {
 void subscribe(Observer super T> observer);
 }

Slide 28

Slide 28 text

interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } Observable Flowable Flowable vs. Observable interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 }

Slide 29

Slide 29 text

interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable Flowable vs. Observable • 收到 Source 傳來來的資料,可接收多次。

Slide 30

Slide 30 text

interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable Flowable vs. Observable • 收到完成,表⽰示此 Source 已經「成功」的結束,不再傳送任何東⻄西。

Slide 31

Slide 31 text

interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable Flowable vs. Observable • 此 Source 發⽣生錯誤,並結束。

Slide 32

Slide 32 text

Flowable vs. Observable interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable

Slide 33

Slide 33 text

Source Specializations • RxJava 2.0 額外新增了了幾個特別的 Source • 這些 Source 為 Observable 的⼦子集和(subsets)

Slide 34

Slide 34 text

Source Specializations • RxJava 2.0 額外新增了了幾個特別的 Source • 這些 Source 為 Observable 的⼦子集和(subsets) • Single - 回傳 1 或 error • Completable - 回傳 complete 或 error • Maybe - 回傳 0, 1, complete 或 error

Slide 35

Slide 35 text

Single • 只會回傳下列列兩兩種情況: • Succeeds with an item. • Errors • 不⽀支援流量量控制(backpressure)

Slide 36

Slide 36 text

Completable • 只會回傳下列列兩兩種情況: • Completes (no items!) • Errors • 不⽀支援流量量控制(backpressure)

Slide 37

Slide 37 text

Maybe • 只會回傳下列列三種情況: • Succeeds with an item. • Completes with no items. • Errors • 不⽀支援流量量控制(backpressure)

Slide 38

Slide 38 text

Source Specializations • RxJava 2.0 額外新增了了幾個特別的 Source • 這些 Source 為 Observable 的⼦子集和(subsets) • Single - 回傳 1 或 error • Completable - 回傳 complete 或 error • Maybe - 回傳 0, 1, complete 或 error

Slide 39

Slide 39 text

Creating Sources Flowable.just("Hello");
 Flowable.just("Hello", "World");
 
 Observable.just("Hello");
 Observable.just("Hello", "World");
 
 Single.just("Hello");
 
 Maybe.just("Hello");

Slide 40

Slide 40 text

Creating Sources String[] array = {"a", "b"};
 List list = new ArrayList<>(); 
 Flowable.fromArray(array);
 Flowable.fromIterable(list);
 
 Observable.fromArray(array);
 Observable.fromIterable(list);

Slide 41

Slide 41 text

Creating Sources Flowable.fromCallable(() -> "Hello");
 
 Observable.fromCallable(() -> "Hello");
 
 Single.fromCallable(() -> "Hello");
 
 Maybe.fromCallable(() -> "Hello");
 
 Completable.fromCallable(() -> "Ignore!");


Slide 42

Slide 42 text

Creating Sources Flowable.fromCallable(() -> "Hello");
 
 Observable.fromCallable(() -> "Hello");
 
 Single.fromCallable(() -> "Hello");
 
 Maybe.fromCallable(() -> "Hello");
 Maybe.fromAction(() -> System.out.println("Hello"));
 Maybe.fromRunnable(() -> System.out.println("Hello"));
 
 Completable.fromCallable(() -> "Ignore!");
 Completable.fromAction(() -> System.out.println("Hello"));
 Completable.fromRunnable(() -> System.out.println("Hello"));

Slide 43

Slide 43 text

Creating Sources Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 return "Hello";
 }
 }); • Returns an Observable that, when an observer subscribes to it, invokes a function you specify and then emits the value returned from that function. • 當有⼈人訂閱 (subscribe) 時,才發射資料。

Slide 44

Slide 44 text

Creating Sources Observable.create();

Slide 45

Slide 45 text

Creating Sources Observable.create(new ObservableOnSubscribe() {
 @Override
 public void subscribe(ObservableEmitter e) throws Exception {
 e.onNext("Hello");
 e.onComplete();
 }
 });

Slide 46

Slide 46 text

Creating Sources Observable.create(new ObservableOnSubscribe() {
 @Override
 public void subscribe(ObservableEmitter e) throws Exception {
 e.onNext("Hello");
 e.onComplete();
 }
 });

Slide 47

Slide 47 text

Creating Sources Observable.create(new ObservableOnSubscribe() {
 @Override
 public void subscribe(ObservableEmitter e) throws Exception {
 e.onNext("Hello");
 e.onComplete();
 }
 });

Slide 48

Slide 48 text

Creating Sources Observable.create(new ObservableOnSubscribe() {
 @Override
 public void subscribe(ObservableEmitter e) throws Exception {
 e.onNext("Hello");
 e.onComplete();
 }
 });

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Creating Sources Observable.create(e -> {
 e.onNext(“Hello"); e.onNext("World");
 e.onComplete();
 }); • 可以呼叫多次 onNext • (fromCallable 只能呼叫⼀一次 onNext)

Slide 51

Slide 51 text

Creating Sources ExecutorService executor = Executors.newSingleThreadExecutor();
 Observable.create(e -> {
 executor.submit(() -> {
 // ...
 e.onNext(result);
 e.onComplete();
 });
 }); • 可在 background thread 呼叫 onNext,實作 async task。

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Creating Sources OkHttpClient client = // … Request request = // … 
 Observable.create(e -> {
 Call call = client.newCall(request);
 e.setCancellable(() -> call.cancel());
 call.enqueue(new Callback() {
 @Override
 public void onResponse(Call call, Response response) throws IOException {
 e.onNext(response.body().toString());
 e.onComplete();
 }
 
 @Override
 public void onFailure(Call call, IOException ex) {
 e.onError(ex);
 }
 });
 });

Slide 57

Slide 57 text

Creating Sources OkHttpClient client = // … Request request = // … 
 Observable.create(e -> {
 Call call = client.newCall(request);
 e.setCancellable(() -> call.cancel());
 call.enqueue(new Callback() {
 @Override
 public void onResponse(Call call, Response response) throws IOException {
 e.onNext(response.body().toString());
 e.onComplete();
 }
 
 @Override
 public void onFailure(Call call, IOException ex) {
 e.onError(ex);
 }
 });
 }); 當 unsubscribe 時,取消 request

Slide 58

Slide 58 text

Creating Sources Button button = // … Observable.create(e -> {
 e.setCancellable(() -> {
 button.setOnClickListener(null) }); button.setOnClickListener(v -> e.onNext(v));
 });

Slide 59

Slide 59 text

Creating Sources Button button = // … Observable.create(e -> {
 e.setCancellable(() -> {
 button.setOnClickListener(null) }); button.setOnClickListener(v -> e.onNext(v));
 }); • 在 Android 中常會⽤用到,避免 memory leak。

Slide 60

Slide 60 text

Observing Sources

Slide 61

Slide 61 text

Observing Sources interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable

Slide 62

Slide 62 text

Observing Sources interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable

Slide 63

Slide 63 text

• 當開始監聽資料時,會⾺馬上呼叫 onSubscribe。 Observing Sources interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable

Slide 64

Slide 64 text

interface Subscriber {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Subscription s);
 }
 
 interface Subscription {
 void cancel();
 void request(long r);
 } interface Observer {
 void onNext(T t);
 void onComplete();
 void onError(Throwable t);
 void onSubscribe(Disposable d);
 }
 
 interface Disposable {
 void dispose();
 } Observable Flowable • 當開始監聽資料時,會⾺馬上呼叫 onSubscribe。 • Observer 可以利利⽤用 onSubscribe 提供的物件,呼叫 cancel 或是 backpressure。 Observing Sources

Slide 65

Slide 65 text

Observing Sources Observable.just("Hello").subscribe(new Observer() {
 @Override
 public void onNext(String value) {
 
 }
 @Override
 public void onComplete() {
 
 }
 @Override
 public void onError(Throwable e) {
 
 }
 @Override
 public void onSubscribe(Disposable d) {
 
 }
 });

Slide 66

Slide 66 text

Observing Sources Observable.just("Hello").subscribe(new Observer() {
 @Override
 public void onNext(String value) {
 
 }
 @Override
 public void onComplete() {
 
 }
 @Override
 public void onError(Throwable e) {
 
 }
 @Override
 public void onSubscribe(Disposable d) {
 
 }
 });

Slide 67

Slide 67 text

Observing Sources Observable.just("Hello").subscribe(new DisposableObserver() {
 @Override
 public void onNext(String value) {
 } 
 @Override
 public void onComplete() {
 }
 
 @Override
 public void onError(Throwable e) {
 }
 });

Slide 68

Slide 68 text

Observing Sources DisposableObserver observer = new DisposableObserver() {
 @Override
 public void onNext(String value) {
 }
 
 @Override
 public void onComplete() {
 }
 
 @Override
 public void onError(Throwable e) {
 }
 }; 
 Observable.just(“Hello”).subscribe(observer);

Slide 69

Slide 69 text

Observing Sources DisposableObserver observer = new DisposableObserver() {
 @Override
 public void onNext(String value) {
 }
 
 @Override
 public void onComplete() {
 }
 
 @Override
 public void onError(Throwable e) {
 }
 }; 
 Observable.just(“Hello”).subscribe(observer); // 如何 unsubscribe ?

Slide 70

Slide 70 text

Observing Sources DisposableObserver observer = new DisposableObserver() {
 @Override
 public void onNext(String value) {
 }
 
 @Override
 public void onComplete() {
 }
 
 @Override
 public void onError(Throwable e) {
 }
 }; 
 Observable.just(“Hello").subscribe(observer); observer.dispose();

Slide 71

Slide 71 text

Observing Sources Observable o = Observable.just("Hello"); Disposable d = o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 }); d.dispose();

Slide 72

Slide 72 text

Observing Sources Observable o = Observable.just(“Hello"); Disposable d = o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 }); d.dispose(); New Method: subscribeWith

Slide 73

Slide 73 text

Observing Sources Observable o = Observable.just(“Hello"); Disposable d = o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 }); d.dispose(); 回傳 Disposable

Slide 74

Slide 74 text

Observing Sources Observable o = Observable.just(“Hello"); Disposable d = o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 }); d.dispose();

Slide 75

Slide 75 text

Observing Sources Observable o = Observable.just(“Hello”); CompositeDisposable disposables = new CompositeDisposable(); disposables.add(o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 })); disposables.dispose();

Slide 76

Slide 76 text

Observing Sources Observable o = Observable.just(“Hello”); CompositeDisposable disposables = new CompositeDisposable(); disposables.add(o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 })); disposables.dispose();

Slide 77

Slide 77 text

Observing Sources Observable o = Observable.just(“Hello”); CompositeDisposable disposables = new CompositeDisposable(); disposables.add(o.subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) { … }
 @Override
 public void onComplete() { … }
 @Override
 public void onError(Throwable e) { … }
 })); disposables.dispose();

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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 DisposableObserver() { … }); Single s = Single.just("Hello");
 s.subscribeWith(new DisposableObserver() { … }); Completable c = Completable.completed(“Hello");
 c.subscribeWith(new DisposableObserver() { … }); • Flowable 也提供類似的 Disposable。

Slide 80

Slide 80 text

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.

Slide 81

Slide 81 text

Operators String normal = "hello";
 String upperCase = normal.toUpperCase();

Slide 82

Slide 82 text

Operators String normal = "hello";
 String upperCase = normal.toUpperCase(); Observable normal = Observable.just("hello");
 Observable upperCase = normal.map(s -> s.toUpperCase());

Slide 83

Slide 83 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 });

Slide 84

Slide 84 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 }).subscribeOn(Schedulers.computation());

Slide 85

Slide 85 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 }).subscribeOn(Schedulers.computation()); • 在 background thread 做事情。 bg thread

Slide 86

Slide 86 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 }).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); bg thread

Slide 87

Slide 87 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 }).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); • Observer 在 main thread 做事情。 bg thread main thread

Slide 88

Slide 88 text

Operators Observable.fromCallable(new Callable() {
 @Override
 public String call() throws Exception {
 // ...
 return result;
 }
 }).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); • Observer 在 main thread 做事情。 main thread bg thread

Slide 89

Slide 89 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .map() .subscribe(observer);

Slide 90

Slide 90 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .map() .subscribe(observer); main thread

Slide 91

Slide 91 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer);

Slide 92

Slide 92 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); • Order 有關係 computation thread

Slide 93

Slide 93 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer);

Slide 94

Slide 94 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); computation thread

Slide 95

Slide 95 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); ?? thread computation thread

Slide 96

Slide 96 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); computation thread computation thread

Slide 97

Slide 97 text

Operators Observable.fromCallable(() -> {
 return result;
 }).subscribeOn(Schedulers.computation()) .map() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); • 在同⼀一個 Source 中,subscribeOn 是先搶先贏。會依照第⼀一次 subscribeOn 的 schedulers 為準。 computation thread computation thread

Slide 98

Slide 98 text

• RxJava 1.0 Operator: • first() - return Observable • takeFirst() - return Observable Operators

Slide 99

Slide 99 text

• RxJava 1.0 Operator: • first() - return Observable • takeFirst() - return Observable • first():找出第⼀一個 element,如果沒有任何 item 則會回傳 error。 • takeFirst():找出第⼀一個 element,就算沒有拿到任何 item,也會 回傳 succeed。 Operators

Slide 100

Slide 100 text

• RxJava 2.0 Operator: • firstOrError() - return Single • firstElement() - return Maybe Operators

Slide 101

Slide 101 text

• RxJava 2.0 Operator: • firstOrError() - return Single • firstElement() - return Maybe • BJ4 Operators

Slide 102

Slide 102 text

Being Reactive apiService.listToilets()


Slide 103

Slide 103 text

Being Reactive apiService.listToilets() .subscribeOn(Schedulers.io())


Slide 104

Slide 104 text

Being Reactive apiService.listToilets() .subscribeOn(Schedulers.io())
 .toSortedList(this::compareDistance)

Slide 105

Slide 105 text

Being Reactive apiService.listToilets() .subscribeOn(Schedulers.io())
 .toSortedList(this::compareDistance) .observerOn(AndroidSchedulers.mainThread())

Slide 106

Slide 106 text

Being Reactive apiService.listToilets() .subscribeOn(Schedulers.io())
 .toSortedList(this::compareDistance) .observerOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) {
 // Show toilets.
 }
 @Override
 public void onComplete() {/* ignore */}
 @Override
 public void onError(Throwable e) {/* show error */}
 });

Slide 107

Slide 107 text

Being Reactive disposables.add(apiService.listToilets() .subscribeOn(Schedulers.io())
 .toSortedList(this::compareDistance) .observerOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) {
 // Show toilets.
 }
 @Override
 public void onComplete() {/* ignore */}
 @Override
 public void onError(Throwable e) {/* show error */}
 }));

Slide 108

Slide 108 text

Being Reactive // onCreate disposables.add(apiService.listToilets() .subscribeOn(Schedulers.io())
 .toSortedList(this::compareDistance) .observerOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver() {
 @Override
 public void onNext(String value) {
 // Show toilets.
 }
 @Override
 public void onComplete() {/* ignore */}
 @Override
 public void onError(Throwable e) {/* show error */}
 })); // onDestory disposables.dispose();

Slide 109

Slide 109 text

Architecture 2.x • 建立資料流(stream)時使⽤用更更少的資源。 • 監聽資料流時(stream)時負擔更更少。 • 更更有效率的 RxJava

Slide 110

Slide 110 text

RxJava 2 • 撰寫程式的⽬目的是:能夠針對 Android 上的資料或UI變動時,做出應 有反應。 • ⽽而不是去主導或管理理這些變動。

Slide 111

Slide 111 text

Reference • Exploring RxJava 2 for Android
 https://speakerdeck.com/jakewharton/exploring-rxjava-2-for- android-gotocph-october-2016 • RxJava Official wiki
 https://github.com/ReactiveX/RxJava/wiki/What's-different- in-2.0 • Reactive-streams API
 http://www.reactive-streams.org/reactive-streams-1.0.0- javadoc/

Slide 112

Slide 112 text

Reference • Reactive-Streams
 http://www.reactive-streams.org/