本文整理了Java中rx.Observable.switchOnNext()
方法的一些代码示例,展示了Observable.switchOnNext()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.switchOnNext()
方法的具体详情如下:
包路径:rx.Observable
类名称:Observable
方法名:switchOnNext
[英]Converts an Observable that emits Observables into an Observable that emits the items emitted by the most recently emitted of those Observables.
switchOnNext subscribes to an Observable that emits Observables. Each time it observes one of these emitted Observables, the Observable returned by switchOnNext begins emitting the items emitted by that Observable. When a new Observable is emitted, switchOnNext stops emitting items from the earlier-emitted Observable and begins emitting items from the new one. Scheduler: switchOnNext does not operate by default on a particular Scheduler.
[中]将发射可观测项的可观测项转换为发射最近发射的可观测项的可观测项。
switchOnNext订阅一个发出可见光的可见光。每次它观察到其中一个发射的可观测值时,switchOnNext返回的可观测值开始发射该可观测值发射的项目。当一个新的可见光被发射时,switchOnNext停止发射先前发射的可见光中的项目,并开始发射新的可见光中的项目。Scheduler:switchOnNext默认情况下不会在特定的计划程序上运行。
代码示例来源:origin: com.netflix.rxjava/rxjava-core
/**
* Returns a new Observable by applying a function that you supply to each item emitted by the source
* Observable that returns an Observable, and then emitting the items emitted by the most recently emitted
* of these Observables.
* <p>
* <img width="640" height="350" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/switchMap.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code switchMap} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param func
* a function that, when applied to an item emitted by the source Observable, returns an
* Observable
* @return an Observable that emits the items emitted by the Observable returned from applying {@code func} to the most recently emitted item emitted by the source Observable
* @see <a href="https://github.com/Netflix/RxJava/wiki/Transforming-Observables#switchmap">RxJava wiki: switchMap</a>
*/
public final <R> Observable<R> switchMap(Func1<? super T, ? extends Observable<? extends R>> func) {
return switchOnNext(map(func));
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_52() throws Exception {
Observable<String> alice = speak(
"To be, or not to be: that is the question", 110);
Observable<String> bob = speak(
"Though this be madness, yet there is method in't", 90);
Observable<String> jane = speak(
"There are more things in Heaven and Earth, " +
"Horatio, than are dreamt of in your philosophy", 100);
Random rnd = new Random();
Observable<Observable<String>> quotes = just(
alice.map(w -> "Alice: " + w),
bob.map(w -> "Bob: " + w),
jane.map(w -> "Jane: " + w))
.flatMap(innerObs -> just(innerObs)
.delay(rnd.nextInt(5), SECONDS));
Observable
.switchOnNext(quotes)
.subscribe(System.out::println);
Sleeper.sleep(Duration.ofSeconds(10));
}
内容来源于网络,如有侵权,请联系作者删除!