本文共 2995 字,大约阅读时间需要 9 分钟。
前言
前面写过RxJava类似观察者模式,但是一直没提到RxJava如何取消订阅,今天就来学习一下.
Subscription
RxJava中有个叫做Subscription
的接口,可以用来取消订阅.
public interface Subscription { /** * Stops the receipt of notifications on the {@link Subscriber} that was registered when this Subscription * was received. ** This allows unregistering an {@link Subscriber} before it has finished receiving all events (i.e. before * onCompleted is called). */ void unsubscribe(); /** * Indicates whether this {@code Subscription} is currently unsubscribed. * * @return {@code true} if this {@code Subscription} is currently unsubscribed, {@code false} otherwise */ boolean isUnsubscribed();}
从上面可以看到,我们只需要调用unsubscribe
就可以取消订阅,那么如何得到一个Subscription
对象呢?
其实很简单:
Observable.subscribe()
方法可以返回一个Subscription
的对象,即我们每次订阅都会返回. 感觉Subscription
就像一个订单,你下单了就会生成一个订单,而你也可以用这个订单取消订单. OK,内容其实不多,那么来练习一下吧.
实战
我先写了以下代码来测试:
Subscription subscription = Observable.just("Hello subscription") .subscribe(new Action1() { @Override public void call(String s) { System.out.println(s); } });System.out.println(subscription.isUnsubscribed());subscription.unsubscribe();System.out.println(subscription.isUnsubscribed());
在我想来输出的日志应该是这样的:
Hello subscriptionfalsetrue
但是,结果出乎我的意料,我运行之后是这样的:
Hello subscriptiontruetrue
诶?不对啊,明明我没有取消订阅啊,怎么就true了呢?
接下去我进源码探索了一下发现:
在Observable.subscribe()
里有这么一段代码: if (!(subscriber instanceof SafeSubscriber)) { // assign to `observer` so we return the protected version subscriber = new SafeSubscriber(subscriber);}
它会把我们传递的subscriber
转成SafeSubscriber
,接下去跟进发现:
public void onCompleted() { if (!done) { done = true; try { actual.onCompleted(); } catch (Throwable e) { // we handle here instead of another method so we don't add stacks to the frame // which can prevent it from being able to handle StackOverflow Exceptions.throwIfFatal(e); // handle errors if the onCompleted implementation fails, not just if the Observable fails _onError(e); } finally { // auto-unsubscribe unsubscribe(); } }}
原来它在finally里自动取消了订阅!!
那么让onCompleted晚点执行就行了:
Subscription subscription = Observable.just("Hello subscription") .subscribeOn(Schedulers.newThread())//起线程 .subscribe(new Action1() { @Override public void call(String s) { try { Thread.sleep(5000);//睡5秒,延迟一下 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(s); } });//默认subscription 调用完System.out.println(subscription.isUnsubscribed());subscription.unsubscribe();System.out.println(subscription.isUnsubscribed());
Log:
falsetrue
注意,取消订阅了之后Hello subscription
并不会打印,你想,你取消了订阅报纸,报社还会给你发报纸么?
好了关于Subscription
暂时就探索到这里了.
安利:
我的微信公众号:转载地址:https://blog.csdn.net/u014400934/article/details/101489960 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!