single
C#版本下Single已经过时,推荐使用C#的async和await语法糖来替代。
Rxjava(及其派生出的RxGroovy、RxScala)开发了一个Observable变体叫做Single。
Single与Observable类似,不同之处在于不再发送数据序列、不发送或发送无数个数据,而是发送一个数据或一个错误通知。
因此,Single不再需要订阅到Observable上的三个函数(onNext、onERROR、onCommpleted),而只需要订阅两个函数:
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 (onNext, onError, 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可使用操作符进行操作。有些操作符可同时用于Observable和Single对象,因此可混用二者:
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:
操作符 |
返回值 |
描述 |
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 |
将发送Single的Single转换为 发送另一个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:
组合
concat 和concatWith
另一种描述:
create
delay
也可在特定Scheduler上下文中执行delay操作:
doOnError
doOnSuccess
error
flatMap
flatMapObservable
from
也可指定一个Scheduler类型的参数:
just
map
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对象发送的数据合并起来由一个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):
observeOn
onErrorReturn
subscribeOn
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:
也可在指定Scheduler上下文上进行计时:
You can also specify a particular Scheduler for the timer to operate on:
另外超时时也可以切换到候补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:
同样也可以指定Scheduler上下文:
This, too, has a Scheduler-specific version:
toObservable
zip 和zipWith
相关阅读
安卓Activity 跳转的标记深度思考FLAG_ACTIVITY_NEW_T
知识储备参考地址:http://blog.csdn.net/ljz2009y/article/details/26621815 FLAG_ACTIVITY_NEW_TASK标记了FLAG_ACTIVITY_NEW_TA
FLAG_ACTIVITY_CLEAR_TOP与FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TOP与FLAG_ACTIVITY_SINGLE_TOP 如果一个应用的栈自底向上是Fisrt—Second—Third 此时,当Third去调用First
【 FPGA 】FIR 滤波器之Single-rate FIR滤波器的系数
首先要明确什么是单速率 FIR 滤波器? The basic FIR filter core is a single-rate (input sample rate = output sample rate) fi
required a single bean, but 2 were found:
我先将我的错误描述贴出来 描述大概的意思是,BaseService只需要一个bean,但是发现了两个bean。 于是我们可以查看一下BaseService
CreateMutex、WaitForSingleObject、ReleaseMutex——
CreateMutexCreateMutex作用是找出当前系统是否已经存在指定进程的实例。如果没有则创建一个互斥体。互斥对象是系统内核维护的一