您的位置:首页 > Web前端

深入理解RxJava的Side Effect Methods

2016-07-27 17:50 387 查看
2017年9月7日更新:

显示隐藏加载框,现在应该使用 doOnSubscribe 和 doOnUnsubscribe,原因请看 Rxjava 中 onError 时 doOnTerminate 不调用的原因探索

以上 基于 RxJava 1.3.0

1. RxJava’s Side Effect Methods

RxJava’s Side Effect Methods

这篇文章可以让你对Side Effect Methods有更深的了解,建议好好品读一遍。

X 3

重要的话说三遍。

时间有限,在这里我只说一些关键点。

是什么?

RxJava’s Observable class has plenty of methods that can be used to transform the stream of emitted items to the kind of data that you need. Those methods are at the very core of RxJava and form a big part of it’s attraction.

But there are other methods, that do not change the stream of items in any way – I call those methods side effect methods.

简单来讲就是说RxJava有两种方法,第一种就是我们常见的方法,即我们常说的操作符,如:create,just,from,map,flatmap,Subscribe 等等等等,这些方法直接影响操作着Observable发射过来的数据流,这些有用的方法构成了RxJava的核心。

还有一些方法,比如:doOnSubscribe(),doOnNext(),doOnTerminate(),doOnCompleted() 等等,这些方法是在那些核心操作发生的时候被触发,并不会对Observable产生丝毫影响,有人叫它Side Effect Methods,我叫它“附属方法“,因为它们附属在核心方法上,并不对核心方法产生影响。

The side effect methods themselves (doOnNext(), doOnCompleted() and so on) do return an Observable. That’s to keep the interface fluent. But the returned Observable is of the same type and emits the same items as the source Observable.

这些方法如:doOnNext(), doOnCompleted() and so on等虽然返回一个Observable,但是却并不会影响原来的Observable,这样只是为了让(That’s to keep the interface fluent)方法调用更加流畅而已。



怎么用?

我挑几个我用过的场景讲,

加载网络数据时LoadingDialog的show和hide。

在doOnSubscribe()里show,在doOnTerminate()的时候hide。

doOnSubscribe()发生在subscribe()动作发生的时候之前,doOnTerminate()发生在onCompleted()和onError()执行之前,也就是当前Observable的结束时候。

doOnSubscribe:

/**
* Modifies the source {@code Observable} so that it invokes the given action when it is subscribed from
* its subscribers. Each subscription will result in an invocation of the given action except when the
* source {@code Observable} is reference counted, in which case the source {@code Observable} will invoke
* the given action for the first subscription.
* @param subscribe
*            the action that gets called when an observer subscribes to this {@code Observable}
* @return the source {@code Observable} modified so as to call this Action when appropriate
*/
public final Observable<T> doOnSubscribe(final Action0 subscribe) {
return lift(new OperatorDoOnSubscribe<T>(subscribe));
}


doOnTerminate

/**
* Modifies the source Observable so that it invokes an action when it calls {@code onCompleted} or
* {@code onError}.
* This differs from {@code finallyDo} in that this happens <em>before</em> the {@code onCompleted} or
* {@code onError} notification.
*
* @param onTerminate
*            the action to invoke when the source Observable calls {@code onCompleted} or {@code onError}
* @return the source Observable with the side-effecting behavior applied
* @see #finallyDo(Action0)
*/
public final Observable<T> doOnTerminate(final Action0 onTerminate) {
Action1<T> onNext = Actions.empty();
Action1<Throwable> onError = Actions.toAction1(onTerminate);

Observer<T> observer = new ActionSubscriber<T>(onNext, onError, onTerminate);

return lift(new OperatorDoOnEach<T>(observer));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rxjava