io.reactivex.Flowable.firstOrError()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(119)

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

Flowable.firstOrError介绍

[英]Returns a Single that emits only the very first item emitted by this Flowable or signals a NoSuchElementException if this Flowable is empty.

Backpressure: The operator honors backpressure from downstream and consumes the source Publisher in an unbounded manner (i.e., without applying backpressure). Scheduler: firstOrError does not operate by default on a particular Scheduler.
[中]返回仅发出此可流动项发出的第一个项的单个项,或者如果此可流动项为空,则发出NoTouchElementException信号。
背压:操作员接受来自下游的背压,并以无限制的方式(即不施加背压)消耗源发布服务器。调度程序:默认情况下,firstOrError不会在特定调度程序上运行。

代码示例

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

@Test
  public void noReentrantDispose() {

    final AtomicInteger cancelCalled = new AtomicInteger();

    final BehaviorProcessor<Integer> p = BehaviorProcessor.create();
    p.doOnCancel(new Action() {
      @Override
      public void run() throws Exception {
        cancelCalled.incrementAndGet();
        p.onNext(2);
      }
    })
    .firstOrError()
    .subscribe()
    .dispose();

    assertEquals(1, cancelCalled.get());
  }
}

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

@Test
public void firstOrErrorError() {
  Flowable.error(new RuntimeException("error"))
    .firstOrError()
    .test()
    .assertNoValues()
    .assertErrorMessage("error")
    .assertError(RuntimeException.class);
}

代码示例来源:origin: apache/incubator-gobblin

public void testBase(InstrumentedExtractorBase<String, String> extractor)
  throws DataRecordException, IOException {
 RecordStreamWithMetadata<String, String> stream = extractor.recordStream(new AtomicBoolean(false));
 RecordEnvelope<String> r = (RecordEnvelope<String>) stream.getRecordStream().firstOrError().blockingGet();
 Map<String, Long> metrics = MetricsHelper.dumpMetrics(extractor.getMetricContext());
 Assert.assertEquals(metrics.get(MetricNames.ExtractorMetrics.RECORDS_READ_METER), Long.valueOf(1));
 Assert.assertEquals(metrics.get(MetricNames.ExtractorMetrics.RECORDS_FAILED_METER), Long.valueOf(0));
 Assert.assertEquals(metrics.get(MetricNames.ExtractorMetrics.EXTRACT_TIMER), Long.valueOf(1));
 Assert.assertEquals(MetricsHelper.dumpTags(extractor.getMetricContext()).get("construct"),
   Constructs.EXTRACTOR.toString());
}

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

@Test
public void firstOrErrorMultipleElements() {
  Flowable.just(1, 2, 3)
    .firstOrError()
    .test()
    .assertNoErrors()
    .assertValue(1);
}

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

@Test
  public void firstOrErrorErrorFlowable() {
    Flowable.error(new RuntimeException("error"))
      .firstOrError()
      .toFlowable()
      .test()
      .assertNoValues()
      .assertErrorMessage("error")
      .assertError(RuntimeException.class);
  }
}

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

@Test
public void firstOrErrorNoElement() {
  Flowable.empty()
    .firstOrError()
    .test()
    .assertNoValues()
    .assertError(NoSuchElementException.class);
}

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

@Test
public void firstOrErrorOneElement() {
  Flowable.just(1)
    .firstOrError()
    .test()
    .assertNoErrors()
    .assertValue(1);
}

代码示例来源:origin: TeamNewPipe/NewPipe

public Completable updateChannelInfo(final ChannelInfo info) {
  final Function<List<SubscriptionEntity>, CompletableSource> update = new Function<List<SubscriptionEntity>, CompletableSource>() {
    @Override
    public CompletableSource apply(@NonNull List<SubscriptionEntity> subscriptionEntities) {
      if (DEBUG) Log.d(TAG, "updateChannelInfo() called with: subscriptionEntities = [" + subscriptionEntities + "]");
      if (subscriptionEntities.size() == 1) {
        SubscriptionEntity subscription = subscriptionEntities.get(0);
        // Subscriber count changes very often, making this check almost unnecessary.
        // Consider removing it later.
        if (!isSubscriptionUpToDate(info, subscription)) {
          subscription.setData(info.getName(), info.getAvatarUrl(), info.getDescription(), info.getSubscriberCount());
          return Completable.fromRunnable(() -> subscriptionTable().update(subscription));
        }
      }
      return Completable.complete();
    }
  };
  return subscriptionTable().getSubscription(info.getServiceId(), info.getUrl())
      .firstOrError()
      .flatMapCompletable(update);
}

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

@Test
public void firstOrErrorNoElementFlowable() {
  Flowable.empty()
    .firstOrError()
    .toFlowable()
    .test()
    .assertNoValues()
    .assertError(NoSuchElementException.class);
}

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

@Test
public void firstOrErrorMultipleElementsFlowable() {
  Flowable.just(1, 2, 3)
    .firstOrError()
    .toFlowable()
    .test()
    .assertNoErrors()
    .assertValue(1);
}

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

@Test
public void firstOrErrorOneElementFlowable() {
  Flowable.just(1)
    .firstOrError()
    .toFlowable()
    .test()
    .assertNoErrors()
    .assertValue(1);
}

代码示例来源:origin: Polidea/RxAndroidBle

.firstOrError()
.subscribeWith(disposableSingleObserverFromEmitter(emitter));

代码示例来源:origin: akarnokd/RxJava2Jdk8Interop

/**
 * Returns a CompletionStage that signals the first element of the Flowable
 * or a NoSuchElementException if the Flowable is empty.
 * @param <T> the value type
 * @return the Function to be used via {@code Flowable.to}.
 */
public static <T> Function<Flowable<T>, CompletionStage<T>> first() {
  return f -> {
    CompletableFuture<T> cf = new CompletableFuture<>();
    f.firstOrError().subscribe(cf::complete, cf::completeExceptionally);
    return cf;
  };
}

代码示例来源:origin: io.smallrye.reactive/smallrye-converter-rxjava2

@Override
public <X> Single fromPublisher(Publisher<X> publisher) {
  return Flowable.fromPublisher(publisher).firstOrError();
}

代码示例来源:origin: instacart/truetime-android

/**
 * Initialize TrueTime
 * Use this if you want to resolve the NTP Pool address to individual IPs yourself
 *
 * See https://github.com/instacart/truetime-android/issues/42
 * to understand why you may want to do something like this.
 *
 * @param resolvedNtpAddresses list of resolved IP addresses for an NTP
 * @return Observable of detailed long[] containing most important parts of the actual NTP response
 * See RESPONSE_INDEX_ prefixes in {@link SntpClient} for details
 */
public Single<long[]> initializeNtp(List<InetAddress> resolvedNtpAddresses) {
  return Flowable.fromIterable(resolvedNtpAddresses)
      .compose(performNtpAlgorithm())
      .firstOrError();
}

代码示例来源:origin: instacart/truetime-android

/**
 * Initialize TrueTime
 * A single NTP pool server is provided.
 * Using DNS we resolve that to multiple IP hosts (See {@link #initializeNtp(List)} for manually resolved IPs)
 *
 * Use this instead of {@link #initializeRx(String)} if you wish to also get additional info for
 * instrumentation/tracking actual NTP response data
 *
 * @param ntpPool NTP pool server e.g. time.apple.com, 0.us.pool.ntp.org
 * @return Observable of detailed long[] containing most important parts of the actual NTP response
 * See RESPONSE_INDEX_ prefixes in {@link SntpClient} for details
 */
public Single<long[]> initializeNtp(String ntpPool) {
  return Flowable
     .just(ntpPool)
     .compose(resolveNtpPoolToIpAddresses())
     .compose(performNtpAlgorithm())
     .firstOrError();
}

代码示例来源:origin: TrustWallet/trust-wallet-android-source

public Single<Wallet> find() {
    return walletRepository
        .getDefaultWallet()
        .onErrorResumeNext(walletRepository
            .fetchWallets()
            .to(single -> Flowable.fromArray(single.blockingGet()))
            .firstOrError()
            .flatMapCompletable(walletRepository::setDefaultWallet)
            .andThen(walletRepository.getDefaultWallet()))
        .observeOn(AndroidSchedulers.mainThread());
  }
}

相关文章

Flowable类方法