RxJava2-Disposable
RxJava2-Disposable
文章目录
- RxJava2-Disposable
- Disposable
- CreateEmitter---onNext/onError/onComplete
- DisposableHelper
- CreateEmitter
Disposable
public interface Disposable {/*** Dispose the resource, the operation should be idempotent.*/void dispose();/*** Returns true if this resource has been disposed.* @return true if this resource has been disposed*/boolean isDisposed();
}
Disposable主要是对事件流的处理;有两个接口方法,一个是用来中断事件流,一个是用来判断事件流是否中断。
目前接触到的实现类有ObservableCreate中的静态内部类CreateEmitter,里面实现了这两个方法
CreateEmitter—onNext/onError/onComplete
@Overridepublic void setDisposable(Disposable d) {DisposableHelper.set(this, d);}@Overridepublic void dispose() {DisposableHelper.dispose(this);}@Overridepublic boolean isDisposed() {return DisposableHelper.isDisposed(get());}
第一个方法是ObservableEmitter中的方法,也被CreateEmitter实现了。
这里涉及到了一个DisposableHelper
DisposableHelper
public enum DisposableHelper implements Disposable {/*** The singleton instance representing a terminal, disposed state, don't leak it.*/DISPOSED;/*** Checks if the given Disposable is the common {@link #DISPOSED} enum value.* @param d the disposable to check* @return true if d is {@link #DISPOSED}*/public static boolean isDisposed(Disposable d) {//判断Disposable类型的变量的引用是否等于DISPOSED//即判断该连接器是否被中断return d == DISPOSED;}/*** Atomically sets the field and disposes the old contents.* @param field the target field* @param d the new Disposable to set* @return true if successful, false if the field contains the {@link #DISPOSED} instance.*/public static boolean set(AtomicReference<Disposable> field, Disposable d) {for (;;) {Disposable current = field.get();if (current == DISPOSED) {if (d != null) {d.dispose();}return false;}if (field.compareAndSet(current, d)) {if (current != null) {current.dispose();}return true;}}}/*** Atomically sets the field to the given non-null Disposable and returns true* or returns false if the field is non-null.* If the target field contains the common DISPOSED instance, the supplied disposable* is disposed. If the field contains other non-null Disposable, an IllegalStateException* is signalled to the RxJavaPlugins.onError hook.* * @param field the target field* @param d the disposable to set, not null* @return true if the operation succeeded, false*/public static boolean setOnce(AtomicReference<Disposable> field, Disposable d) {ObjectHelper.requireNonNull(d, "d is null");if (!field.compareAndSet(null, d)) {d.dispose();if (field.get() != DISPOSED) {reportDisposableSet();}return false;}return true;}/*** Atomically replaces the Disposable in the field with the given new Disposable* but does not dispose the old one.* @param field the target field to change* @param d the new disposable, null allowed* @return true if the operation succeeded, false if the target field contained* the common DISPOSED instance and the given disposable (if not null) is disposed.*/public static boolean replace(AtomicReference<Disposable> field, Disposable d) {for (;;) {Disposable current = field.get();if (current == DISPOSED) {if (d != null) {d.dispose();}return false;}if (field.compareAndSet(current, d)) {return true;}}}/*** Atomically disposes the Disposable in the field if not already disposed.* @param field the target field* @return true if the current thread managed to dispose the Disposable*/public static boolean dispose(AtomicReference<Disposable> field) {Disposable current = field.get();Disposable d = DISPOSED;if (current != d) {//这里会把field给设为DISPOSEDcurrent = field.getAndSet(d);if (current != d) {if (current != null) {current.dispose();}return true;}}return false;}/*** Verifies that current is null, next is not null, otherwise signals errors* to the RxJavaPlugins and returns false.* @param current the current Disposable, expected to be null* @param next the next Disposable, expected to be non-null* @return true if the validation succeeded*/public static boolean validate(Disposable current, Disposable next) {if (next == null) {RxJavaPlugins.onError(new NullPointerException("next is null"));return false;}if (current != null) {next.dispose();reportDisposableSet();return false;}return true;}/*** Reports that the disposable is already set to the RxJavaPlugins error handler.*/public static void reportDisposableSet() {RxJavaPlugins.onError(new ProtocolViolationException("Disposable already set!"));}/*** Atomically tries to set the given Disposable on the field if it is null or disposes it if* the field contains {@link #DISPOSED}.* @param field the target field* @param d the disposable to set* @return true if successful, false otherwise*/public static boolean trySet(AtomicReference<Disposable> field, Disposable d) {if (!field.compareAndSet(null, d)) {if (field.get() == DISPOSED) {d.dispose();}return false;}return true;}@Overridepublic void dispose() {// deliberately no-op}@Overridepublic boolean isDisposed() {return true;}
这是一个枚举类,只有一个值DISPOSED,dispose()方法中会把一个原子引用field设为DISPOSED,即标记为中断状态。因此后面通过isDisposed()方法即可以判断连接器是否被中断。
再去CreateEmitter中看看发射事件中的调用
CreateEmitter
static final class CreateEmitter<T>extends AtomicReference<Disposable>implements ObservableEmitter<T>, Disposable {private static final long serialVersionUID = -3434801548987643227L;final Observer<? super T> observer;CreateEmitter(Observer<? super T> observer) {this.observer = observer;}@Overridepublic void onNext(T t) {if (t == null) {onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));return;}if (!isDisposed()) {observer.onNext(t);}}@Overridepublic void onError(Throwable t) {if (!tryOnError(t)) {RxJavaPlugins.onError(t);}}@Overridepublic boolean tryOnError(Throwable t) {if (t == null) {t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");}if (!isDisposed()) {try {observer.onError(t);} finally {dispose();}return true;}return false;}@Overridepublic void onComplete() {if (!isDisposed()) {try {observer.onComplete();} finally {dispose();}}}@Overridepublic void setDisposable(Disposable d) {DisposableHelper.set(this, d);}@Overridepublic void setCancellable(Cancellable c) {setDisposable(new CancellableDisposable(c));}@Overridepublic ObservableEmitter<T> serialize() {return new SerializedEmitter<T>(this);}@Overridepublic void dispose() {DisposableHelper.dispose(this);}@Overridepublic boolean isDisposed() {return DisposableHelper.isDisposed(get());}}
可以看到,再我们常用的三个方法onNext/onError/onComplete方法中都会先使用isDisposed()来判断一下,然后决定是否发射事件;所以我们在Observer的onSubscribe(Disposable)方法中拿到这个发射器对象,然后在我们调用dispose()方法,设置标签,这样在下发下一个事件的时候都会先调用isDisposed()来判断,从而截断事件流。
RxJava2-Disposable相关推荐
- Android RxBinding
In the previous tutorials, we discussed RxJava and some of its operators. Today we will discuss the ...
- Android rxjava2的disposable
rxjava+retrofit处理网络请求 在使用rxjava+retrofit处理网络请求的时候,一般会采用对观察者进行封装,实现代码复用和拓展.可以参考我的这篇文章:rxjava2+retrofi ...
- java disposable_Android rxjava2的disposable
rxjava+retrofit处理网络请求 在使用rxjava+retrofit处理网络请求的时候,一般会采用对观察者进行封装,实现代码复用和拓展.可以参考我的这篇文章:rxjava2+retrofi ...
- 【安卓】rxjava2的disposable
rxjava+retrofit处理网络请求 在使用rxjava+retrofit处理网络请求的时候,一般会采用对观察者进行封装,实现代码复用和拓展.可以参考我的这篇文章:rxjava2+retrofi ...
- Rxjava Disposable解除订阅(Retrofit2+Rxjava2主动取消网络请求)
Disposable类 dispose():主动解除订阅(如果使用Retrofit2+Rxjava2,调用dispose会主动取消网络请求,在本文的后半部分) isDisposed():查询是否解除订 ...
- RxJava2 如何使工作线程在Disposable.dispose后完成流程
在使用Rxjava2时,特别要注意内存泄漏.所以一般在activity或者fragment销毁时调用disposable.dispose来取消订阅.但在之前的工作中遇到一个问题,如何确保你在work ...
- Rxjava2关于Disposable你应该知道的事
关于disposable Disposable类 dispose():主动解除订阅 isDisposed():查询是否解除订阅 true 代表 已经解除订阅 rxjava虽然好用,但是总所周知,容易造 ...
- Android每周一个学习计划——RxJava2 0的学习使用
序言:蜗壳已经退役一年多了,但是还是抵不住蜗壳在NBA界的影响力,最近NBA流行向"蜗壳挑战",事情起源于蜗壳给IT和北境之王设定了新赛季的挑战,然后众多球星也纷纷向蜗壳讨要挑战. ...
- 如何从RxJava升级到RxJava2
如何从RxJava升级到RxJava2. RxJava2已经推出有一年半的时间,由于之前RxJava已经在现有项目中广泛使用,而RxJava2在除了很多命名外并没有太多革新,所以相信有很多人跟我一样都 ...
- java concat和 的区别,RxJava2 merge和concat 区别
merge merge 将全部订阅 Observable,但是谁先完成谁先通知,如果大家完成时间一样,按顺序调用 public static void rxJava2() { Observable.m ...
最新文章
- MySQL02-升级
- 你可能不知道的Shell
- java sql sum函数的使用方法_SQL常用汇总函数用法说明
- CSS 实例之翻转图片
- 高端android手机,高端机型很难选择?这几款手机就很不错,你肯定有中意的
- Blazor确认复选框组件
- 和尚感谢你,要避开,这样才能求到福
- 计算机科学的endnote格式,基于国家标准的 EndNote 输出样式模板
- 微信无法绑定手机号的解决方案
- 纯css实现二级下拉菜单
- 使用HBuilder打包App教程(图文教程)
- canvas mdn_MDN文档 canvas教程笔记
- 第9章 数据库完整性
- zip和tgz以及exe的区别
- 嵌入式Linux从入门到精通之第八节:GTK+详解
- 商业级、工业级、军品级、宇航级CPU有着不同标准
- php h5 调用摄像头_利用html5调用本地摄像头拍照上传图片
- 为什么很多智能锁没有防破坏报警功能?
- 【天地图】使用天地图api绘制GeoJson数据
- 落户经验分享---单列计划落户档案派遣,存档流程说明