RxJava(及其派生出的RxGroovyRxScala)开发了一个Observable变体叫做Single

SingleObservable类似,不同之处在于不再发送数据序列、不发送或发送无数个数据,而是发送一个数据或一个错误通知。

因此,Single不再需要订阅到Observable上的三个函数(onNextonErroronCommpleted),而只需要订阅两个函数:

onSuccess

  Single通过这个方法发送唯一的数据

onError:

  Single通过这个方法发送导致无法发送数据的异常

Single只能调用其中的一个方法,且只能调用一次。一旦调用了其中之一,Single及其上的订阅结束运行。

RxJava (and its derivatives like RxGroovy & RxScala) has developed an Observable variant called “Single.”

A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification.

For this reason, instead of subscribing to a Single with the three methods you use to respond to notifications from an Observable (onNextonError, and onCompleted), you only use two methods to subscribe:

onSuccess

a Single passes this method the sole item that the Single emits

onError

a Single passes this method the Throwable that caused the Single to be unable to emit an item

A Single will call only one of these methods, and will only call it once. Upon calling either method, the Single terminates and the subscription to it ends.

使用Single操作进行组合Composition via Single Operators

Observable类似,Single可使用操作符进行操作。有些操作符可同时用于ObservableSingle对象,因此可混用二者:

Like Observables, Singles can be manipulated by means of a variety of operators. Some operators also allow for an interface between the Observable world and the Single world so that you can mix the two varieties:

操作符

返回值

描述

compose

Single

创建自定义操作符

allows you create a custom operator

concat concatWith

Observable

将多个Single对象的发送数据进行合并,同Observable

concatenates the items emitted by multiple Singles as Observable emissions

create

Single

显示指定订阅方法创建一个Single对象

create a Single from scratch by calling subscriber methods explicitly

delay

Single

将发送数据沿着Single对象的时间线向前移动

move the emission of an item from a Single forward in time

doOnError

Single

返回一个Single对象调用onError方法时触发相应的订阅

returns a Single that also calls a method you specify when it calls onError

doOnSuccess

Single

返回一个Single对象onSuccess方法时触发相应的订阅

returns a Single that also calls a method you specify when it calls onSuccess

error

Single

返回一个Single,并立即向订阅者发送错误通知

returns a Single that immediately notifies subscribers of an error

flatMap

Single

返回一个Single对象将另一个Single的发送数据进行变换

returns a Single that is the result of a function applied to an item emitted by a Single

flatMapObservable

Observable

返回一个Observable对象将另一个Single的发送数据进行变换

returns an Observable that is the result of a function applied to an item emitted by a Single

from

Single

Future转换为Single对象

converts a Future into a Single

just

Single

返回发送特定数据的Single对象

returns a Single that emits a specified item

map

Single

返回一个Single对象,将另一个Single对象发送的数据进行变换

returns a Single that emits the result of a function applied to the item emitted by the source Single

merge

Single

将发送SingleSingle转换为

发送另一个Single发送数据的Single

converts a Single that emits a second Single into a Single that emits the item emitted by the second Single

merge and mergeWith

Observable

将多个Single对象发送的数据合并到一个Observable对象

merges the items emitted by multiple Singles as Observable emissions

observeOn

Single

指示Single在指定的Scheduler上下文上调用订阅者的方法。

instructs the Single to call the subscriber methods on a particular Scheduler

onErrorReturn

Single

将错误通知Single转化为发送特定数据的Single

converts a Single that makes an error notification into a Single that emits a specified item

subscribeOn

Single

指示Single在特定Scheduler上下文中执行操作

instructs the Single to operate on a particular Scheduler

timeout

Single

返回一个Single,当另一个Single在指定时间内没有发送数据,则发出错误通知。

returns a Single that makes an error notification if the source Single does not emit a value in a specified time period

toSingle

Single

将只发送一个数据项的Observable对象转换为Single对象。

converts an Observable that emits a single item into a Single that emits that item

toObservable

Observable

Single对象转换为Observable,发送Single对象的数据项后结束执行。

converts a Single into an Observable that emits the item emitted by the Single and then completes

zip and zipWith

Single

返回一个Single对象,将多个Single发送的数据项进行变换后,进行发送。

returns a Single that emits an item that is the result of a function applied to items emitted by two or more other Singles

下图解释这些操作符。

The following sections of this page will give marble diagrams that explain these operators schematically. This diagram explains how Singles are represented in marble diagrams:

Single

组合

concat concatWith

Single

另一种描述:

Single

create

Single

delay

Single

也可在特定Scheduler上下文中执行delay操作:

Single

doOnError

Single

doOnSuccess

Single

error

Single

flatMap

Single

flatMapObservable

Single

from

Single

也可指定一个Scheduler类型的参数:

Single

just

Single

map

Single

merge mergeWith

merge的一个版本将一个发送Single对象的Single转换为发送另一个Single对象发送的数据:

One version of merge takes a Single that emits a second Single and converts it into a Single that emits the item emitted by that second Single:

Single

另一个版本将多个Single对象发送的数据合并起来由一个Observable进行发送:

Another version takes two or more Singles and merges them into an Observable that emits the items emitted by the source Singles (in an arbitrary order):

Single

observeOn

Single

onErrorReturn

Single

subscribeOn

Single

timeout

timerout将返回一个Single对象,当关联的Single对象在被订阅后的指定时间段内没有发送数据,则这个Single对象发送错误通知。一个版本可通过指定时间单位设置超时时间:

Timeout will cause a Single to abort with an error notification if it does not emit an item in a specified period of time after it is subscribed to. One version allows you to set this time out by means of a number of specified time units:

Single

也可在指定Scheduler上下文上进行计时:

You can also specify a particular Scheduler for the timer to operate on:

Single

另外超时时也可以切换到候补Single,而不是发送错误通知:

A version of the timeout operator allows you to switch to a backup Single rather than sending an error notification if the timeout expires:

Single

同样也可以指定Scheduler上下文:

This, too, has a Scheduler-specific version:

Single

toObservable

Single

zip zipWith

Single

 

相关文章: