RxJava

作者: 王增辉 | 来源:发表于2016-08-29 00:46 被阅读57次

ReactiveX

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.

RxJava

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.
Docs

Why
Old
new Thread() {
    @Override
    public void run() {
        super.run();
        for (File folder : folders) {
            File[] files = folder.listFiles();
            for (File file : files) {
                if (file.getName().endsWith(".png")) {
                    final Bitmap bitmap = getBitmapFromFile(file);
                    getActivity().runOnUiThread(new Runnable() { 
                        @Override
                        public void run() {
                            imageCollectorView.addImage(bitmap);
                        }
                    });
                }
            }
        }
    }
}.start();
New
Observable.from(folders) 
    .flatMap(new Func1<File, Observable<File>>() {
        @Override
        public Observable<File> call(File file) {
            return Observable.from(file.listFiles());
        }
    })
    .filter(new Func1<File, Boolean>() {
        @Override
        public Boolean call(File file) {
            return file.getName().endsWith(".png");
        }
    })
    .map(new Func1<File, Bitmap>() {
        @Override
        public Bitmap call(File file) {
            return getBitmapFromFile(file);
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Bitmap>() {
        @Override
        public void call(Bitmap bitmap) {
            imageCollectorView.addImage(bitmap);
        }
    });
How
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.9'

RxAndroid
Android specific bindings for RxJava.
This module adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free. More specifically, it provides a Scheduler
that schedules on the main thread or any given Looper
.

Operators
create
Observable.create(New Observable.OnSubscribe<String>()) {
    @Override
    public void call(Subscriber<? super String> subscriber) {
        subscribe.onNext("hello world");
    }
}
map
map(new Func1<JSONObject, Object>() {
    @Override
    public Object call(JSONObject jsonObject) {
        return null;
    }
})
flatMap
flatMap(new Func1<Object, Observable<?>>() {
    @Override
    public Observable<?> call(Object o) {
        return null;
    }
})
zip
observable = Observable.zip(method1(), method2(), new Func2<Object, Object, Object>() {
    @Override
    public Object call(Object object1, Object object2) {
        return null;
    }
})
Scheduler

subscribeOn
observeOn

With Retrofit

in build.gradle

compile "com.squareup.retrofit2:retrofit:${libs.retrofit}"
compile "com.squareup.retrofit2:converter-gson:${libs.retrofit}"
compile "com.squareup.retrofit2:adapter-rxjava:${libs.retrofit}"
compile "com.squareup.okhttp3:okhttp:${libs.okhttp}"
compile "com.squareup.okio:okio:${libs.okio}"

in retrofit interface

@GET("event/list")
Observable<JsonObject> queryEventList(@Query("loc") int locId,
                                      @Query("day_type") String dayType,
                                      @Query("type") String type,
                                      @Query("start") int start,
                                      @Query("count") int count);

in presenter

Subscription subscription = DoubanApiClient.getInstance()
        .queryEventList(108288, "week", "all", 0, 20)
        .subscribeOn(Schedulers.io())
        .doOnNext(jsonObject -> System.out.print("zhainan " + jsonObject.toString()))
        .map(DoubanEventModel::parse)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            eventModels -> {
                mModels.clear();
                mModels.addAll(eventModels);
                if (hasMvpView()) {
                    getMvpView().showContent(MvpLceRecyclerView.NotifyType.DataSetChanged, 0, 0);
                    getMvpView().showEmptyView(mModels.size() == 0);
                }
            }
         );

bind(subscription);

References

给 Android 开发者的 RxJava 详解
RxJava 与 Retrofit 结合的最佳实践

ReactiveX/RxJava文档中文版
深入浅出RxJava
RxJava使用场景
在Android Studio中使用Lambda表达式
Retrofit2.0 改进
Retrofit 添加默认参数

相关文章

网友评论

      本文标题:RxJava

      本文链接:https://www.haomeiwen.com/subject/adagsttx.html