RXJAVA2.0的变化

翻译 2018年04月15日 14:16:40
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
   compile 'io.reactivex.rxjava2:rxandroid:2.0.1'         //RXjava2.0版本
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'  //rxjava2 retrofit2

package com.bwie.rxjava2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.subscribers.DisposableSubscriber;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @BindView(R.id.btn_diff_01)
    Button btnDiff01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_diff_01, R.id.btn_diff_02, R.id.btn_diff_03})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_diff_01:
                Observable observable = Observable.just(1, 2, 3);
                // 观察者Observer多了一个onSubscribe方法
                Observer observer = new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                };

                // Subscriber没有了onStart方法
                Subscriber subscriber = new Subscriber() {

                    @Override
                    public void onSubscribe(Subscription s) {

                    }

                    @Override
                    public void onNext(Object o) {

                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                };

                observable.subscribe(observer);
                // Observable不能subscribe这个Subscribe观察者了
//                observable.subscribe(subscriber);

                // 多了一个被观察者
                Flowable flowable = Flowable.just(1, 2);
                // flowable不能订阅Observer
//                flowable.subscribe(observer);
                flowable.subscribe(subscriber);
                break;
            case R.id.btn_diff_02:
//                Observer没有Action1,Action0
//                Observable.just(1, 2, 3)
//                        .subscribe(new ac)

//                Flowable.just(1,2,3)
//                        .subscribe(new Act)

                // Func1--> Function
                Flowable.just(1, 2, 3)
                        .map(new Function<Integer, String>() {
                            @Override
                            public String apply(Integer integer) throws Exception {
                                return null;
                            }
                        })
                        .subscribe(new Subscriber<String>() {
                            @Override
                            public void onSubscribe(Subscription s) {

                            }

                            @Override
                            public void onNext(String s) {

                            }

                            @Override
                            public void onError(Throwable t) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

                break;

            case R.id.btn_diff_03:
                Log.i(TAG, "onViewClicked: 点击了第三个按钮");
//                Observable.just(1,2,4)
//                        .subscribe(new Observer<Integer>() {
//                            @Override
//                            public void onSubscribe(Disposable d) {
//
//                            }
//
//                            @Override
//                            public void onNext(Integer integer) {
//                                Log.i(TAG, "onNext: "+integer);
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//
//                            }
//
//                            @Override
//                            public void onComplete() {
//
//                            }
//                        });


                // Flowable需要指定背压策略
//                Flowable.just(1, 2, 3)
                Flowable.create(new FlowableOnSubscribe<Integer>() {
                    @Override
                    public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
                        emitter.onNext(1);
                        emitter.onNext(2);
                        emitter.onNext(3);
                        emitter.onNext(4);
                        emitter.onNext(5);
                    }
                },

                        // 背压策略,数据发送过快后来不及处理的直接丢弃
//                        BackpressureStrategy.DROP
                        // 背压策略,带Buffer的一种策略
                        BackpressureStrategy.BUFFER
                )
                        .subscribe(new Subscriber<Integer>() {
                            @Override
                            public void onSubscribe(Subscription s) {
                                s.request(Integer.MAX_VALUE);
                            }

                            @Override
                            public void onNext(Integer integer) {
                                Log.i(TAG, "onNext: " + integer);
                            }

                            @Override
                            public void onError(Throwable t) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
//                Flowable.just(1, 2, 3)
//                        .subscribe(new DisposableSubscriber<Integer>() {
//                            @Override
//                            public void onNext(Integer integer) {
//                                Log.i(TAG, "onNext: "+integer);
//                            }
//
//                            @Override
//                            public void onError(Throwable t) {
//
//                            }
//
//                            @Override
//                            public void onComplete() {
//
//                            }
//                        });
                break;
        }
    }
}

业务变化带来应用架构变化

-
  • 1970年01月01日 08:00

Rxjava2.0初探

Rxjava2.0初探
  • Mr_zhang0101
  • Mr_zhang0101
  • 2017-07-07 11:09:48
  • 780

RxJava2.0在android开发中的实用总结

  • 2017年12月05日 19:20
  • 1.5MB
  • 下载

RxJava2.0的使用

RxJava的基本使用
  • day14
  • day14
  • 2017-08-07 09:35:30
  • 260

全面剖析Rxjava2.0的前世今生

引言 随着项目的不断壮大,我们的业务越来越复杂,引入RxJava框架是迟早的事,这段时间正好花了几天时间来认真学习了Rxjava一把,好记性不如烂笔头,还是乖乖记录下来学习的所得所感。 一 、Rxja...
  • monkey646812329
  • monkey646812329
  • 2017-07-31 15:14:40
  • 1018

RxJava2.0使用(二)

RxJava中默认就是在哪个线程中发生事件就在哪个线程中消费事件,但是很多时候我们都有这样的需求比如联网请求数据,我们需要在子线程中去联网获取数据,获取到数据之后在主线程中更新UI。这时候就需要切换线...
  • mingyunxiaohai
  • mingyunxiaohai
  • 2017-07-12 18:31:38
  • 763

Retrofit2.0与RXJava2.0最新结合:

retrofit的基本配置跟之前的没有多少改变,可以看一下这篇文章: 需要的jar包: retrofit2.0的基本配置与缓存 return new Retrofit.Builder() ...
  • changsimeng
  • changsimeng
  • 2017-04-06 15:42:50
  • 1431

RxJava2.0在android开发中的实用操作符总结

  • 2018年04月14日 12:33
  • 1.49MB
  • 下载

从零开始的RxJava2.0教程(三)响应式的好处

1. 前言在第一篇中,我介绍了RxJava的基础知识。第二篇中,我向你展示了操作符的强大之处。这一篇,我将向你介绍响应式的优点。 2. 错误处理到目前为止,我都没怎么介绍 onComplete 和 ...
  • qq_35064774
  • qq_35064774
  • 2016-11-07 13:14:34
  • 5574

RxJava2.0的使用-2

指定工作线程 操作符 map 操作符 flatMap 操作符 concatMap 操作符 zip 操作符 buffer 操作符 take 操作符 distinct 操作符 filter 操作符 sam...
  • u013491677
  • u013491677
  • 2017-05-25 18:40:43
  • 813
收藏助手
不良信息举报
您举报文章:RXJAVA2.0的变化
举报原因:
原因补充:

(最多只允许输入30个字)