RxJava2輕鬆入門

沉默的範大叔發表於2017-02-20

Demo地址

RxJava2Demo

本Demo旨在幫助從未接觸過RxJava的同學直接入坑RxJava2,如絲般順滑,萬水千山總是情,留個star行不行?

RxJava & RxAndroid (2.0版)

定義

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

一個在 Java VM 上使用可觀測的序列來組成非同步的、基於事件的程式的庫

初學者如果看到這個準確但晦澀的定義肯定一臉懵逼,不過我們我們只要把握重點即可:

  • 非同步
  • 基於事件
  • 觀察者模式

RxAndroid - Android specific bindings for RxJava 2.This module adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free.

RxAndroid在RxJava的基礎上新增了最少的類使得開發Android應用中的響應式元件更加的容易和自由

特點

簡潔,並不是指程式碼量上的那種簡潔,而是邏輯上的簡潔,隨著程式邏輯變得越來越複雜,它依然能夠保持簡潔。

Github

Hello world

新增依賴

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'複製程式碼

簡單版本

//簡單版本
private void helloWorldSimple() {
    //建立消費者,消費者接受一個String型別的事件
    Consumer<String> consumer = new Consumer<String>() {
        @Override
        public void accept(String s) throws Exception {
            Log.d(TAG, s);
        }
    };
    //被觀察者發出Hello World, 並且指定該事件的消費者為consumer
    Observable.just("Hello World").subscribe(consumer);
}複製程式碼

執行結果

D/MainActivity: Hello World複製程式碼

複雜版本

private void helloWorldComplex() {
    //Observer可以看做Consumer的完整版
    Observer<String> observer = new Observer<String>() {

        //當Observable呼叫subscribe方法時會回撥該方法
        @Override
        public void onSubscribe(Disposable d) {
            Log.d(TAG, "onSubscribe: ");
        }

        //onSubscribe方法後呼叫
        @Override
        public void onNext(String value) {
            Log.d(TAG, "onNext: " + value);
        }
        //這裡沒有出錯,沒有被呼叫
        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "onError: ");
        }

        //onNext之後呼叫
        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete: ");
        }
    };
    //被觀察者發出Hello World, 並且指定該事件的觀察者為observer
    Observable.just("Hello World").subscribe(observer);
}複製程式碼

執行結果

D/MainActivity: onSubscribe: 
D/MainActivity: onNext: Hello World
D/MainActivity: onComplete: 複製程式碼

變態版本

private void helloWorldPlus() {
    //建立一個觀察者
    Observer<String> observer = new Observer<String>() {

        //當Observable呼叫subscribe方法時會回撥該方法
        @Override
        public void onSubscribe(Disposable d) {
            Log.d(TAG, "onSubscribe: ");
        }

        //onSubscribe方法後呼叫
        @Override
        public void onNext(String value) {
            Log.d(TAG, "onNext: " + value);
        }
        //這裡沒有出錯,沒有被呼叫
        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "onError: ");
        }

        //onNext之後呼叫
        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete: ");
        }
    };

    //建立一個Observable
    Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> e) throws Exception {
            e.onNext("Hello World");//會呼叫到觀察者的onNext
            e.onComplete();//會呼叫到觀察者的onComplete
        }
    });

    observable.subscribe(observer);
}複製程式碼

執行結果

D/MainActivity: onSubscribe: 
D/MainActivity: onNext: Hello World
D/MainActivity: onComplete: 複製程式碼

filter操作符

你早上去吃早餐,師傅是被觀察者,說我們這有包子,饅頭,腸粉,春捲,餃子,炒粉,你仔細想了想,發現你是最喜歡餃子的,所以把其他的都排除掉,
於是你就吃到了餃子。

private void filter() {
    //把Consumer可以看做精簡版的Observer
    Consumer<String> consumer = new Consumer<String>() {
        //accept可以簡單的看做onNext
        @Override
        public void accept(String s) throws Exception {
            Log.d(TAG, "accept: " + s);//這裡只能吃上餃子
        }
    };

    Observable.just("包子", "饅頭", "腸粉", "春捲", "餃子", "炒粉")
            .filter(new Predicate<String>() {
                @Override
                public boolean test(String s) throws Exception {
                    Log.d(TAG, "test: " + s);
                    return s.equals("餃子");//只允許餃子通過測試
                }
            })
            .subscribe(consumer);
}複製程式碼

執行結果

D/MainActivity: test: 包子
D/MainActivity: test: 饅頭
D/MainActivity: test: 腸粉
D/MainActivity: test: 春捲
D/MainActivity: test: 餃子
D/MainActivity: accept: 餃子
D/MainActivity: test: 炒粉複製程式碼

map操作符

map操作符能夠完成資料型別的轉換。 以下程式碼展示了一個Student到Developer的轉換。

private void map() {
    Observer<Developer> observer = new Observer<Developer>() {
        @Override
        public void onSubscribe(Disposable d) {
            Log.d(TAG, "onSubscribe: ");
        }

        //觀察者接收到一個Developer
        @Override
        public void onNext(Developer value) {
            Log.d(TAG, "onNext: "  + value.toString());
        }

        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "onError: ");
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete: ");
        }
    };

    Student student = new Student();
    student.setName("Leon");
    student.setAge(18);
    //map操作符,從Student型別轉換成Developer
    Observable.just(student).map(new Function<Student, Developer>() {
        @Override
        public Developer apply(Student student) throws Exception {
            Log.d(TAG, "apply: " + student.toString());
            Developer developer = new Developer();
            developer.setName(student.getName());
            developer.setAge(student.getAge());
            developer.setSkill("Android");
            return developer;
        }
    }).subscribe(observer);
}複製程式碼

執行結果

D/MainActivity: onSubscribe: 
D/MainActivity: apply: Student{name='Leon', age=18}
D/MainActivity: onNext: Developer{name='Leon', age=18, skill='Android'}
D/MainActivity: onComplete: 複製程式碼

flatmap操作符

flatmap能夠鏈式地完成資料型別的轉換和加工。

遍歷一個學校中所有班級所有學生

private void flatmapClassToStudent() {

    Observable.fromIterable(new School().getClasses())

            //輸入是Class型別,輸出是ObservableSource<Student>型別
            .flatMap(new Function<Class, ObservableSource<Student>>() {

                //輸入是Class型別,輸出是ObservableSource<Student>型別
                @Override
                public ObservableSource<Student> apply(Class aClass) throws Exception {
                    Log.d(TAG, "apply: " + aClass.toString());
                    return Observable.fromIterable(aClass.getStudents());
                }
            }).subscribe(
            new Observer<Student>() {
                @Override
                public void onSubscribe(Disposable d) {
                    Log.d(TAG, "onSubscribe: ");
                }

                @Override
                public void onNext(Student value) {
                    Log.d(TAG, "onNext: " + value.toString());
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
}複製程式碼

執行結果

D/MainActivity: onSubscribe: 
D/MainActivity: apply: Class0
D/MainActivity: onNext: Student{name='Class0_0', age=18}
D/MainActivity: onNext: Student{name='Class0_1', age=18}
D/MainActivity: onNext: Student{name='Class0_2', age=18}
D/MainActivity: apply: Class1
D/MainActivity: onNext: Student{name='Class1_0', age=18}
D/MainActivity: onNext: Student{name='Class1_1', age=18}
D/MainActivity: onNext: Student{name='Class1_2', age=18}
D/MainActivity: apply: Class2
D/MainActivity: onNext: Student{name='Class2_0', age=18}
D/MainActivity: onNext: Student{name='Class2_1', age=18}
D/MainActivity: onNext: Student{name='Class2_2', age=18}複製程式碼

遍歷一個學校所有班級所有組的所有學生

private void flatmapClassToGroupToStudent() {
    Observable.fromIterable(new School().getClasses())
            //輸入是Class型別,輸出是ObservableSource<Group>型別
            .flatMap(new Function<Class, ObservableSource<Group>>() {
                @Override
                public ObservableSource<Group> apply(Class aClass) throws Exception {
                    Log.d(TAG, "apply: " + aClass.toString());
                    return Observable.fromIterable(aClass.getGroups());
                }
            })
            //輸入型別是Group,輸出型別是ObservableSource<Student>型別
            .flatMap(new Function<Group, ObservableSource<Student>>() {
                @Override
                public ObservableSource<Student> apply(Group group) throws Exception {
                    Log.d(TAG, "apply: " + group.toString());
                    return Observable.fromIterable(group.getStudents());
                }
            })
            .subscribe(
                    new Observer<Student>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "onSubscribe: ");
                        }

                        @Override
                        public void onNext(Student value) {
                            Log.d(TAG, "onNext: " + value.toString());
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
}複製程式碼

執行結果

D/MainActivity: onSubscribe: 
D/MainActivity: apply: Class0
D/MainActivity: apply: Group0
D/MainActivity: onNext: Student{name='Group0_0', age=18}
D/MainActivity: onNext: Student{name='Group0_1', age=18}
D/MainActivity: onNext: Student{name='Group0_2', age=18}
D/MainActivity: apply: Group1
D/MainActivity: onNext: Student{name='Group1_0', age=18}
D/MainActivity: onNext: Student{name='Group1_1', age=18}
D/MainActivity: onNext: Student{name='Group1_2', age=18}
D/MainActivity: apply: Group2
D/MainActivity: onNext: Student{name='Group2_0', age=18}
D/MainActivity: onNext: Student{name='Group2_1', age=18}
D/MainActivity: onNext: Student{name='Group2_2', age=18}
D/MainActivity: apply: Class1
D/MainActivity: apply: Group0
D/MainActivity: onNext: Student{name='Group0_0', age=18}
D/MainActivity: onNext: Student{name='Group0_1', age=18}
D/MainActivity: onNext: Student{name='Group0_2', age=18}
D/MainActivity: apply: Group1
D/MainActivity: onNext: Student{name='Group1_0', age=18}
D/MainActivity: onNext: Student{name='Group1_1', age=18}
D/MainActivity: onNext: Student{name='Group1_2', age=18}
D/MainActivity: apply: Group2
D/MainActivity: onNext: Student{name='Group2_0', age=18}
D/MainActivity: onNext: Student{name='Group2_1', age=18}
D/MainActivity: onNext: Student{name='Group2_2', age=18}
D/MainActivity: apply: Class2
D/MainActivity: apply: Group0
D/MainActivity: onNext: Student{name='Group0_0', age=18}
D/MainActivity: onNext: Student{name='Group0_1', age=18}
D/MainActivity: onNext: Student{name='Group0_2', age=18}
D/MainActivity: apply: Group1
D/MainActivity: onNext: Student{name='Group1_0', age=18}
D/MainActivity: onNext: Student{name='Group1_1', age=18}
D/MainActivity: onNext: Student{name='Group1_2', age=18}
D/MainActivity: apply: Group2
D/MainActivity: onNext: Student{name='Group2_0', age=18}
D/MainActivity: onNext: Student{name='Group2_1', age=18}
D/MainActivity: onNext: Student{name='Group2_2', age=18}複製程式碼

執行緒排程

關於RxJava的執行緒排程,初學者只需要掌握兩個api就夠夠的啦。

subscribeOn

指定Observable在一個指定的執行緒排程器上建立。只能指定一次,如果指定多次則以第一次為準

observeOn

指定在事件傳遞,轉換,加工和最終被觀察者接受發生在哪一個執行緒排程器。可指定多次,每次指定完都在下一步生效。

常用執行緒排程器型別

  • Schedulers.single() 單執行緒排程器,執行緒可複用
  • Schedulers.newThread() 為每個任務建立新的執行緒
  • Schedulers.io() 處理io密集型任務,內部是執行緒池實現,可自動根據需求增長
  • Schedulers.computation() 處理計算任務,如事件迴圈和回撥任務
  • AndroidSchedulers.mainThread() Android主執行緒排程器

示例

private void scheduleThreads() {
    Observable.create(
            new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    Log.d(TAG, "subscribe: " + Thread.currentThread().getName());
                    e.onNext("Hello Leon Fan");
                    e.onComplete();
                }
            })
            //指定subscribe方法在io執行緒池中呼叫
            .subscribeOn(Schedulers.io())
            //指定onNext方法 onComplete的方法在新建的執行緒中呼叫
            .observeOn(Schedulers.newThread())
            .subscribe(
                    new Observer<String>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "onSubscribe: " + Thread.currentThread().getName());
                        }

                        @Override
                        public void onNext(String value) {
                            Log.d(TAG, "onNext: " + Thread.currentThread().getName() + " " + value);
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {
                            Log.d(TAG, "onComplete: " + Thread.currentThread().getName());
                        }
                    });

}複製程式碼

執行結果

D/MainActivity: onSubscribe: main
D/MainActivity: subscribe: RxCachedThreadScheduler-4
D/MainActivity: onNext: RxNewThreadScheduler-1 Hello Leon Fan
D/MainActivity: onComplete: RxNewThreadScheduler-1複製程式碼

如果將示例中的.observeOn(Schedulers.newThread())改成AndroidSchedulers.mainThread(),則執行結果如下:

D/MainActivity: onSubscribe: main
D/MainActivity: subscribe: RxCachedThreadScheduler-5
D/MainActivity: onNext: main Hello Leon Fan
D/MainActivity: onComplete: main複製程式碼

RxJava與Retrofit整合

我們做一個Demo通過網路請求獲取豆瓣電影Top10的列表來展示RxJava和Retrofit的整合的姿勢。

RxJava2輕鬆入門

Retrofit整合

新增依賴

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' 官方adapter僅支援rxjava1.0
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'複製程式碼

建立網路介面

public interface Api {
    @GET("top250")
    Observable<MovieBean> listTop250(@Query("start") int start, @Query("count") int count);
}複製程式碼

實現Api

public class MovieRetrofit {

    private static MovieRetrofit sMovieRetrofit;
    private final Api mApi;

    public static MovieRetrofit getInstance() {
        if (sMovieRetrofit == null) {
            synchronized (MovieRetrofit.class) {
                if (sMovieRetrofit == null) {
                    sMovieRetrofit = new MovieRetrofit();
                }
            }
        }
        return sMovieRetrofit;
    }

    private MovieRetrofit() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.douban.com/v2/movie/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        mApi = retrofit.create(Api.class);
    }

    public Api getApi() {
        return mApi;
    }
}複製程式碼

傳送網路請求重新整理列表

<!--新增網路許可權-->
<uses-permission android:name="android.permission.INTERNET"/>

Observable<MovieBean> movieBeanObservable = MovieRetrofit.getInstance().getApi().listTop250(0, 10);
movieBeanObservable.subscribeOn(Schedulers.io())//在io執行緒池中執行map
        //將網路的結果轉換成我們要的電影名的列表
        .map(new Function<MovieBean, List<String>>() {
            @Override
            public List<String> apply(MovieBean movieBean) throws Exception {
                List<String> array = new ArrayList<String>();
                for (int i = 0; i < movieBean.getSubjects().size(); i++) {
                    String title = movieBean.getSubjects().get(i).getTitle();
                    array.add(title);
                }
                return array;
            }
        })
        .observeOn(AndroidSchedulers.mainThread())//在主執行緒中執行onNext
        .subscribe(new Observer<List<String>>() {

        ......

            @Override
            public void onNext(List<String> value) {
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MovieListActivity.this, android.R.layout.simple_list_item_1, value);
                setListAdapter(arrayAdapter);
            }
            ......
        });複製程式碼

參考

本人旨在幫助從未接觸過RxJava的童鞋直接入坑RxJava2.0,更多使用姿勢請自行參考其他資料學習。

相關文章