转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/106720158
本文出自【赵彦军的博客】
文章目录
- 依赖接入
- Flowable
- Single
- Maybe
- BackpressureStrategy
- 线程切换
依赖接入
1 2 | implementation 'io.reactivex.rxjava3:rxandroid:3.0.0' implementation "io.reactivex.rxjava3:rxjava:3.0.4" |
Flowable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //java 方式 Flowable.just(1) .subscribe(new Consumer<Integer>() { @Override public void accept(Integer integer) throws Throwable { } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Throwable { } }); //或者用 Lambda 简写 Flowable.just(1) .subscribe( it -> { }, throwable -> { }); |
range 一组序列数据
1 2 3 4 5 6 7 8 | Flowable.range(0, 4) .subscribe(it -> { //结果 0 1 2 3 }, throwable -> { }); |
Single
Single只发射单个数据或错误事件,即使发射多个数据,后面发射的数据也不会处理。
只有
SingleEmitter
1 2 3 4 5 6 7 8 9 10 | public interface SingleEmitter<@NonNull T> { void onSuccess(@NonNull T t); void onError(@NonNull Throwable t); void setDisposable(@Nullable Disposable d); void setCancellable(@Nullable Cancellable c); boolean isDisposed(); boolean tryOnError(@NonNull Throwable t); } |
示例1
1 2 3 4 5 6 7 8 9 10 11 12 | Single.create(new SingleOnSubscribe<Integer>() { @Override public void subscribe(@NonNull SingleEmitter<Integer> emitter) throws Throwable { emitter.onSuccess(1); } }) .subscribe(integer -> { }, throwable -> { }); |
示例2
1 2 3 4 5 6 | Single.just(1) .subscribe(integer -> { }, throwable -> { }); |
Maybe
Maybe 是 RxJava2.x 之后才有的新类型,可以看成是Single和Completable的结合。
Maybe 也只能发射单个事件或错误事件,即使发射多个数据,后面发射的数据也不会处理。
只有
1 2 3 4 5 6 7 8 9 10 11 | public interface MaybeEmitter<@NonNull T> { void onSuccess(@NonNull T t); void onError(@NonNull Throwable t); void onComplete(); void setDisposable(@Nullable Disposable d); void setCancellable(@Nullable Cancellable c); boolean isDisposed(); boolean tryOnError(@NonNull Throwable t); } |
实例1
1 2 3 4 5 6 7 8 9 10 11 12 | Maybe.create(new MaybeOnSubscribe<Integer>() { @Override public void subscribe(@NonNull MaybeEmitter<Integer> emitter) throws Throwable { emitter.onSuccess(1); emitter.onComplete(); } }) .subscribe(integer -> { }, throwable -> { }); |
实例2
1 2 3 4 5 6 | Maybe.just(1) .subscribe(integer -> { }, throwable -> { }); |
BackpressureStrategy
背压策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public enum BackpressureStrategy { /** * The {@code onNext} events are written without any buffering or dropping. * Downstream has to deal with any overflow. * <p><center>[wp_ad_camp_4]</center></p><p>Useful when one applies one of the custom-parameter onBackpressureXXX operators. */ MISSING, /** * Signals a {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException MissingBackpressureException} * in case the downstream can't keep up. */ ERROR, /** * Buffers <em>all</em> {@code onNext} values until the downstream consumes it. */ BUFFER, /** * Drops the most recent {@code onNext} value if the downstream can't keep up. */ DROP, /** * Keeps only the latest {@code onNext} value, overwriting any previous value if the * downstream can't keep up. */ LATEST } |
- MISSING 策略则表示通过 Create 方法创建的 Flowable 没有指定背压策略,不会对通过 OnNext 发射的数据做缓存或丢弃处理,需要下游通过背压操作符
- BUFFER 策略则在还有数据未下发完成时就算上游调用onComplete或onError也会等待数据下发完成
- LATEST 策略则当产生背压时仅会缓存最新的数据
- DROP 策略为背压时丢弃背压数据
- ERROR 策略是背压时抛出异常调用onError
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Flowable.create(new FlowableOnSubscribe<Long>() { @Override public void subscribe(@NonNull FlowableEmitter<Long> emitter) throws Throwable { emitter.onNext(1L); emitter.onNext(2L); emitter.onComplete(); } }, BackpressureStrategy.DROP) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(it -> { }, throwable -> { }); |
线程切换
RxUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.example.stream import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.FlowableTransformer import io.reactivex.rxjava3.core.MaybeTransformer import io.reactivex.rxjava3.core.ObservableTransformer import io.reactivex.rxjava3.core.SingleTransformer import io.reactivex.rxjava3.schedulers.Schedulers /** * @author yanjun.zhao * @time 2020/6/12 8:39 PM * @desc */ object RxUtil { /** * 线程切换 */ fun <T> maybeToMain(): MaybeTransformer<T, T> { return MaybeTransformer { upstream -> upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) } } /** * 线程切换 */ fun <T> singleToMain(): SingleTransformer<T, T> { return SingleTransformer { upstream -> upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) } } /** * 线程切换 */ fun <T> flowableToMain(): FlowableTransformer<T, T> { return FlowableTransformer { upstream -> upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) } } fun <T> observableToMain(): ObservableTransformer<T, T> { return ObservableTransformer { upstream -> upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) } } } |
具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package com.example.stream import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.core.Maybe import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Single class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Single.just(1) .map { //运行在子线程 it } .compose(RxUtil.singleToMain()) //线程转换 .subscribe( { //运行在主线程 }, { it.printStackTrace() } ) Maybe.just(1) .map { //运行在子线程 it } .compose(RxUtil.maybeToMain()) //线程转换 .subscribe( { //运行在主线程 }, { it.printStackTrace() } ) Flowable.just(1) .map { //运行在子线程 it } .compose(RxUtil.flowableToMain()) //线程转换 .subscribe( { //运行在主线程 }, { it.printStackTrace() } ) Observable.just(1) .map { //运行在子线程 it } .compose(RxUtil.observableToMain()) //线程转换 .subscribe( { it -> //运行在主线程 }, { it.printStackTrace() } ) } } |