RXJAVA2.0的变化

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;
        }
    }
}
阅读更多

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