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

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

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

Observable.blockingSingle介绍

[英]If this Observable completes after emitting a single item, return that item, otherwise throw a NoSuchElementException.

Scheduler: blockingSingle does not operate by default on a particular Scheduler. Error handling: If the source signals an error, the operator wraps a checked Exceptioninto RuntimeException and throws that. Otherwise, RuntimeExceptions and Errors are rethrown as they are.
[中]如果此可观察项在发出单个项后完成,则返回该项,否则抛出NoTouchElementException。
调度程序:blockingSingle默认情况下不会在特定调度程序上运行。错误处理:如果源发出错误信号,操作员将选中的异常包装到RuntimeException中并抛出该异常。否则,运行时异常和错误将按原样重新启动。

代码示例

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = subject.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = subject.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = subject.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public Integer apply(Integer v) throws Exception {
    Observable.just(1).delay(10, TimeUnit.SECONDS).blockingSingle();
    return v;
  }
})

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

@Test(expected = IllegalArgumentException.class)
public void testMapWithIssue417() {
  Observable.just(1).observeOn(Schedulers.computation())
      .map(new Function<Integer, Integer>() {
        @Override
        public Integer apply(Integer arg0) {
          throw new IllegalArgumentException("any error");
        }
      }).blockingSingle();
}

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

/**
 * We expect IllegalStateException to pass thru map.
 */
@Test(expected = IllegalStateException.class)
public void testErrorPassesThruMap2() {
  Observable.error(new IllegalStateException()).map(new Function<Object, Object>() {
    @Override
    public Object apply(Object i) {
      return i;
    }
  }).blockingSingle();
}

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

@Test(expected = IllegalArgumentException.class)
public void testTakeWithError() {
  Observable.fromIterable(Arrays.asList(1, 2, 3)).take(1)
  .map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer t1) {
      throw new IllegalArgumentException("some error");
    }
  }).blockingSingle();
}

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

@Test(expected = NoSuchElementException.class)
public void blockingSingleEmpty() {
  Observable.empty().blockingSingle();
}

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

@Test
public void testDoOnEach() {
  final AtomicReference<String> r = new AtomicReference<String>();
  String output = Observable.just("one").doOnNext(new Consumer<String>() {
    @Override
    public void accept(String v) {
      r.set(v);
    }
  }).blockingSingle();
  assertEquals("one", output);
  assertEquals("one", r.get());
}

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

@Test
public void doOnTerminateComplete() {
  final AtomicBoolean r = new AtomicBoolean();
  String output = Observable.just("one").doOnTerminate(new Action() {
    @Override
    public void run() {
      r.set(true);
    }
  }).blockingSingle();
  assertEquals("one", output);
  assertTrue(r.get());
}

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

@Test
public void testDoOnCompleted() {
  final AtomicBoolean r = new AtomicBoolean();
  String output = Observable.just("one").doOnComplete(new Action() {
    @Override
    public void run() {
      r.set(true);
    }
  }).blockingSingle();
  assertEquals("one", output);
  assertTrue(r.get());
}

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

@Test
public void testSwitchWhenEmpty() throws Exception {
  final Observable<Integer> o = Observable.<Integer>empty()
      .switchIfEmpty(Observable.fromIterable(Arrays.asList(42)));
  assertEquals(42, o.blockingSingle().intValue());
}

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

@Test
public void reduceIntsObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3);
  int value = o.reduce(new BiFunction<Integer, Integer, Integer>() {
    @Override
    public Integer apply(Integer t1, Integer t2) {
      return t1 + t2;
    }
  }).toObservable().blockingSingle();
  assertEquals(6, value);
}

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

@Test
public void testElementAtObservable() {
  assertEquals(2, Observable.fromArray(1, 2).elementAt(1).toObservable().blockingSingle()
      .intValue());
}

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

@Test
public void testConcatOuterBackpressure() {
  assertEquals(1,
      (int) Observable.<Integer> empty()
          .concatWith(Observable.just(1))
          .take(1)
          .blockingSingle());
}

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

@Test
public void fromIterable() {
  ArrayList<String> items = new ArrayList<String>();
  items.add("one");
  items.add("two");
  items.add("three");
  assertEquals((Long)3L, Observable.fromIterable(items).count().blockingGet());
  assertEquals("two", Observable.fromIterable(items).skip(1).take(1).blockingSingle());
  assertEquals("three", Observable.fromIterable(items).takeLast(1).blockingSingle());
}

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

@Test
public void testElementAtOrDefaultObservable() {
  assertEquals(2, Observable.fromArray(1, 2).elementAt(1, 0).toObservable().blockingSingle().intValue());
}

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

@Test
public void fromArityArgs3() {
  Observable<String> items = Observable.just("one", "two", "three");
  assertEquals((Long)3L, items.count().blockingGet());
  assertEquals("two", items.skip(1).take(1).blockingSingle());
  assertEquals("three", items.takeLast(1).blockingSingle());
}

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

@Test
public void fromArityArgs1() {
  Observable<String> items = Observable.just("one");
  assertEquals((Long)1L, items.count().blockingGet());
  assertEquals("one", items.takeLast(1).blockingSingle());
}

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

@Test
public void fromArray() {
  String[] items = new String[] { "one", "two", "three" };
  assertEquals((Long)3L, Observable.fromArray(items).count().blockingGet());
  assertEquals("two", Observable.fromArray(items).skip(1).take(1).blockingSingle());
  assertEquals("three", Observable.fromArray(items).takeLast(1).blockingSingle());
}

相关文章

Observable类方法