RxJava(八)Disposable和Transformer

匿名 (未验证) 提交于 2019-12-02 20:59:24

Disposable和Transformer

一,Disposable
1.当Observable发送数据,Observer订阅数据时,就形成一个Disposable,可用于在退出时取消订阅。

 Disposable disposable = Observable.just(1,2,3)         .subscribeOn(Schedulers.newThread())         .observeOn(AndroidSchedulers.mainThread())         .map(new Function<Integer, Integer>() {             @Override             public Integer apply(Integer integer) {                 return integer + 1;             }         }).subscribe(new Consumer<Integer>() {                    @Override                    public void accept(Integer integer) throws Exception {                     }                });     } 

2.CompositeDisposable 一个复合订阅容器,用于管理所有的Disposable,方便在退出时取消订阅,避免内存溢出。
通常写在BaseActivity中。RxLifecycle生命周期管理放内存泄露

CompositeDisposable compositeDisposable = new CompositeDisposable();          Disposable disposable = Observable.just(1, 2, 3)                 .subscribeOn(Schedulers.newThread())                 .observeOn(AndroidSchedulers.mainThread())                 .map(new Function<Integer, Integer>() {                     @Override                     public Integer apply(Integer integer) {                         return integer + 1;                     }                 }).subscribe(new Consumer<Integer>() {                     @Override                     public void accept(Integer integer) throws Exception {                      }                 });         //添加Disposable到容器统一管理         compositeDisposable.add(disposable);         //当退出时,如onDestory中调用,取消所有订阅         compositeDisposable.clear(); 

二,Transformer 转换器
可通过一个Transformer转换器将Observable/Flowable/Completable/Maybe/Single转换为另一个Observable/Flowable/Completable/Maybe/Single。
转换器:ObservableTransformer,FlowableTransformer,CompletableTransformer,MaybeTransformer,SingleTransformer。

//数据转换 public static ObservableTransformer<Integer,String> intToStrTransformer(){         //Integer转String的转换器         return new ObservableTransformer<Integer, String>() {             //返回一个发射String数据的Observable             @Override             public ObservableSource<String> apply(Observable<Integer> upstream) {                 return upstream.map(new Function<Integer, String>() {                     @Override                     public String apply(Integer integer) throws Exception {                         return integer.toString();                     }                 });             }         };     }      private void MyTest(){         Observable.just(1,2,3)                 .compose(intToStrTransformer())                 .subscribe(new Consumer<String>() {                     @Override                     public void accept(String s) throws Exception {                      }                 });     } 
//切换线程 public static <T> FlowableTransformer<T,T> transToMain(){          return new FlowableTransformer<T, T>() {             //返回一个在io线程发送数据,在main线程处理数据的Publisher             @Override             public Publisher<T> apply(Flowable<T> upstream) {                 return upstream.subscribeOn(Schedulers.io())                         .observeOn(AndroidSchedulers.mainThread());             }         };     }      private void MyTest(){         Flowable.interval(1000, TimeUnit.MILLISECONDS)                 .onBackpressureBuffer()                 .compose(transToMain())                 .subscribe(new Consumer<Long>() {                     @Override                     public void accept(Long aLong) throws Exception {                         log.d(TAG, "accept: "+aLong);                     }                 });     } 
//缓存数据     public static <T> FlowableTransformer<T,T> transToCache(final String key, final Cache cache){                   return new FlowableTransformer<T, T>() {             @Override             public Publisher<T> apply(Flowable<T> upstream) {                 return upstream.map(new Function<T, T>() {                     @Override                     public T apply(T t) throws Exception {                         cache.put(key,t)                         return t;                     }                 });             }         };                 }      private void MyTest(){         Flowable.interval(1000, TimeUnit.MILLISECONDS)                 .onBackpressureBuffer()                 .compose(MainActivity.<Long>transToCache("key",new Cache()))                 .subscribe(new Consumer<Long>() {                     @Override                     public void accept(Long aLong) throws Exception {                         log.d(TAG, "accept: "+aLong);                     }                 });     } 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!