Rx编程核心: 数据流式处理
每种处理都是一种Operator, 在RxJava中是Function对象; 看完本文即可学会自己定义Operator的技能(应该有卵用)
1
2
3
Flowable.fromArray("Hello", "world")
.contains("world")
.subscribe(System.out::println);
实现业务功能变为数据流的一系列处理转换, 通过Observable及后续的高阶函数可以使我们更清晰的看到数据的转换过程(亦业务实现过程);这也是函数式编程的精髓
上图是Observable基本设计思想
此图有3个要素:
- 被观察者: 用最上面的一根箭头表示; 我们需要自行把业务抽象成数据, Observable封装了一系列工具方法来简化我们创建被观察者过程
- 处理: 数据流; 业务处理的关键, 我们需要对业务原始数据进行加工, 输出结果即为最下面的一根箭头; 我们可以对同一数据反复处理直到观察者满意为止
- 观察者: 数据处理完的最终结果就让观察者使用
Operator实现解析
Rx自带Operator说明
ambWith
blocking*
- blockingFirst
- blockingForEach
- blockingIterable
- blockingLast
- blockingLatest
- blockingMostRecent
- blockingNext
- blockingSingle
- blockingSubscribe
buffer
cache, cacheWithInitialCapacity
cast
compose
concatMap, concatMapDelayError, concatMapEager, concatMapEagerDelayError, concatMapIterable, concatWith
debounce
defaultIfEmpty
delay, delaySubscription
dematerialize
distinct, distinctUntilChanged
do*
- doAfterNext
- doAfterTerminate
- doFinally
- doOnComplete
- doOnDispose
- doOnEach
- doOnError
- doOnLifecycle
- doOnNext
- doOnSubscribe
- doOnTerminate
filter
first,firstElement,firstOrError
flatMap,flatMapCompletable,flatMapIterable,flatMapMaybe,flatMapSingle
forEach,forEachWhile
groupBy, groupJoin
hide
ignoreElements
join
last,lastElement,lastOrError
lift
map
materialize
mergeWith
on*
- onErrorResumeNext
- onErrorReturn
- onErrorReturnItem
- onExceptionResumeNext
- onTerminateDetach
publish
reduce,reduceWith
repeat,repeatUntil,repeatWhen
replay
retry, retryUntil, retryWhen
sample
scan,scanWith
serialize
share
skip, skipLast, skipUntil, skipWhile
sorted
startWith,startWithArray
switch*
- switchIfEmpty
- switchMap
- switchMapDelayError
take, takeLast, takeUntil, takeWhile
throttleFirst, throttleLast, throttleWithTimeout
timeInterval
timeout
timestamp
window
withLatestFrom
zipWith
其它
any
collect, collectInto
contains
count
elementAt, elementAtOrError
isEmpty
observeOn
ofType
single, singleElement, singleOrError
subscribe, subscribeWith
to*
- to
- toFlowable
- toFuture
- toList
- toMap
- toMultimap
- toSortedList