RxJava学习笔记

版权声明:本文为博主(xunming)原创文章,转载请注明出处。 https://blog.csdn.net/diyangxia/article/details/53404587

Bmob中Observable方式

compile 'cn.bmob.android:bmob-sdk:3.5.1'

因为BmobSDK从版本 v3.5.0开始 ,最大的亮点在于:支持Rx链式调用。
PS:bmob-sdk中依赖的rxjava版本是1.1.6,Github上最新的是1.2.3

以下是修改正文字体大小的代码,使用到了concatMap操作符

    /**
     * 链式调用,
     * @see --> https://github.com/bmob/bmob-android-sdk-demo
     * @param size
     */
    void saveTextSize(final int size){
        BmobQuery<SettingBean> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("author", BmobUser.getCurrentUser(UserBean.class));
        Observable<List<SettingBean>> observable = bmobQuery.findObjectsObservable(SettingBean.class);
        observable.concatMap(new Func1<List<SettingBean>, Observable<?>>() {
            @Override
            public Observable<?> call(List<SettingBean> settingBeen) {
                if(settingBeen.size()==0){
                    LogUtil.i(TAG, "concatMap=为0");
                    UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
                    SettingBean settingBean = new SettingBean();
                    settingBean.setAuthor(userBean);
                    settingBean.settSize(size);
                    return settingBean.saveObservable();
                }else{
                    LogUtil.i(TAG, "concatMap=不为0");
                    UserBean userBean = BmobUser.getCurrentUser(UserBean.class);
                    SettingBean settingBean =settingBeen.get(0);
                    settingBean.setAuthor(userBean);
                    settingBean.settSize(size);
                    return settingBean.updateObservable(settingBean.getObjectId());

                }

            }
        }).subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {
                LogUtil.i(TAG, "文字大小修改=onCompleted");
            }

            @Override
            public void onError(Throwable throwable) {
                LogUtil.i(TAG, "文字大小修改=onError");
            }

            @Override
            public void onNext(Object o) {
                LogUtil.i(TAG, "文字大小修改=onNext");
            }
        });


    }

RxJava1使用

compile 'io.reactivex:rxjava:1.2.3'

最基本的用法如下:

 Observable.from(names).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            System.out.println("Hello " + s + "!");
        }

    });

切换线程,示例代码来自这里
有了Rxjava,我们可以跟 new Thread 和 Handler 说byebye了

Observable.empty()  
        .doOnCompleted(new Action0() {
            @Override
            public void call() {
                Log.i("debug", Thread.currentThread().getName());
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnCompleted(new Action0() {
            @Override
            public void call() {
                Log.i("debug", Thread.currentThread().getName());
            }
        })
        .subscribe();

RxJava2使用

这里有Rxjava2和Rxjava1的不同,https://github.com/ReactiveX/RxJava/wiki/What’s-different-in-2.0

http://search.maven.org/#search%7Cga%7C1%7Cio.reactivex.rxjava

这里写图片描述

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'

在build.gradle里加了上面两行依赖后,直接运行就报错如下:
这里写图片描述
简直不知所措,换成另一个项目就正常了(苦笑ing),然后我试了试如下代码:

        Observable.just("哈哈,我在地球上")
                .map(new Function<String, Object>() {
                    @Override
                    public Object apply(String s) throws Exception {
                        return s + "AA";
                    }
                })
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        LogUtil.i("TAG", "ZZZ=" + o.toString());

                    }
                });

使用了map操作符,打印那段文字加上AA后缀

这里写图片描述

没有更多推荐了,返回首页