io.reactivex.Observable.switchOnNext()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(147)

本文整理了Java中io.reactivex.Observable.switchOnNext()方法的一些代码示例,展示了Observable.switchOnNext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.switchOnNext()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:switchOnNext

Observable.switchOnNext介绍

[英]Converts an ObservableSource that emits ObservableSources into an ObservableSource that emits the items emitted by the most recently emitted of those ObservableSources.

switchOnNext subscribes to an ObservableSource that emits ObservableSources. Each time it observes one of these emitted ObservableSources, the ObservableSource returned by switchOnNext begins emitting the items emitted by that ObservableSource. When a new ObservableSource is emitted, switchOnNext stops emitting items from the earlier-emitted ObservableSource and begins emitting items from the new one.

The resulting ObservableSource completes if both the outer ObservableSource and the last inner ObservableSource, if any, complete. If the outer ObservableSource signals an onError, the inner ObservableSource is disposed and the error delivered in-sequence. Scheduler: switchOnNext does not operate by default on a particular Scheduler.
[中]将发出ObservableSource的ObservableSource转换为发出这些ObservableSource中最近发出的项的ObservableSource。
switchOnNext订阅发出ObservableSource的ObservableSource。每次它观察到其中一个发出的ObservableSource时,switchOnNext返回的ObservableSource开始发出该ObservableSource发出的项。当发出新的ObservableSource时,switchOnNext停止从先前发出的ObservableSource发出项目,并开始从新的ObservableSource发出项目。
如果外部可观察资源和最后一个内部可观察资源(如果有)都已完成,则生成的可观察资源将完成。如果外部可观察资源发出一个错误信号,内部可观察资源将被处理,错误将按顺序传递。Scheduler:switchOnNext默认情况下不会在特定的计划程序上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void switchOnNextNull() {
  Observable.switchOnNext(null);
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Converts an ObservableSource that emits ObservableSources into an ObservableSource that emits the items emitted by the
 * most recently emitted of those ObservableSources.
 * <p>
 * <img width="640" height="370" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/switchDo.png" alt="">
 * <p>
 * {@code switchOnNext} subscribes to an ObservableSource that emits ObservableSources. Each time it observes one of
 * these emitted ObservableSources, the ObservableSource returned by {@code switchOnNext} begins emitting the items
 * emitted by that ObservableSource. When a new ObservableSource is emitted, {@code switchOnNext} stops emitting items
 * from the earlier-emitted ObservableSource and begins emitting items from the new one.
 * <p>
 * The resulting ObservableSource completes if both the outer ObservableSource and the last inner ObservableSource, if any, complete.
 * If the outer ObservableSource signals an onError, the inner ObservableSource is disposed and the error delivered in-sequence.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code switchOnNext} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param <T> the item type
 * @param sources
 *            the source ObservableSource that emits ObservableSources
 * @return an Observable that emits the items emitted by the ObservableSource most recently emitted by the source
 *         ObservableSource
 * @see <a href="http://reactivex.io/documentation/operators/switch.html">ReactiveX operators documentation: Switch</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Observable<T> switchOnNext(ObservableSource<? extends ObservableSource<? extends T>> sources) {
  return switchOnNext(sources, bufferSize());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scalarMap() {
  Observable.switchOnNext(Observable.just(Observable.just(1)))
  .test()
  .assertResult(1);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void dispose() {
  TestHelper.checkDisposed(Observable.switchOnNext(
      Observable.just(Observable.just(1)).hide()));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testSwitchWhenOuterCompleteBeforeInner() {
  Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
    @Override
    public void subscribe(Observer<? super Observable<String>> outerObserver) {
      outerObserver.onSubscribe(Disposables.empty());
      publishNext(outerObserver, 50, Observable.unsafeCreate(new ObservableSource<String>() {
        @Override
        public void subscribe(Observer<? super String> innerObserver) {
          innerObserver.onSubscribe(Disposables.empty());
          publishNext(innerObserver, 70, "one");
          publishNext(innerObserver, 100, "two");
          publishCompleted(innerObserver, 200);
        }
      }));
      publishCompleted(outerObserver, 60);
    }
  });
  Observable<String> sampled = Observable.switchOnNext(source);
  sampled.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
  inOrder.verify(observer, times(2)).onNext(anyString());
  inOrder.verify(observer, times(1)).onComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testUnsubscribe() {
  final AtomicBoolean isUnsubscribed = new AtomicBoolean();
  Observable.switchOnNext(
      Observable.unsafeCreate(new ObservableSource<Observable<Integer>>() {
        @Override
        public void subscribe(final Observer<? super Observable<Integer>> observer) {
          Disposable bs = Disposables.empty();
          observer.onSubscribe(bs);
          observer.onNext(Observable.just(1));
          isUnsubscribed.set(bs.isDisposed());
        }
      })
  ).take(1).subscribe();
  assertTrue("Switch doesn't propagate 'unsubscribe'", isUnsubscribed.get());
}
/** The upstream producer hijacked the switch producer stopping the requests aimed at the inner observables. */

代码示例来源:origin: redisson/redisson

/**
 * Converts an ObservableSource that emits ObservableSources into an ObservableSource that emits the items emitted by the
 * most recently emitted of those ObservableSources.
 * <p>
 * <img width="640" height="370" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/switchDo.png" alt="">
 * <p>
 * {@code switchOnNext} subscribes to an ObservableSource that emits ObservableSources. Each time it observes one of
 * these emitted ObservableSources, the ObservableSource returned by {@code switchOnNext} begins emitting the items
 * emitted by that ObservableSource. When a new ObservableSource is emitted, {@code switchOnNext} stops emitting items
 * from the earlier-emitted ObservableSource and begins emitting items from the new one.
 * <p>
 * The resulting ObservableSource completes if both the outer ObservableSource and the last inner ObservableSource, if any, complete.
 * If the outer ObservableSource signals an onError, the inner ObservableSource is disposed and the error delivered in-sequence.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code switchOnNext} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param <T> the item type
 * @param sources
 *            the source ObservableSource that emits ObservableSources
 * @return an Observable that emits the items emitted by the ObservableSource most recently emitted by the source
 *         ObservableSource
 * @see <a href="http://reactivex.io/documentation/operators/switch.html">ReactiveX operators documentation: Switch</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Observable<T> switchOnNext(ObservableSource<? extends ObservableSource<? extends T>> sources) {
  return switchOnNext(sources, bufferSize());
}

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

代码示例来源:origin: ReactiveX/RxJava

Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);

相关文章

Observable类方法