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

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

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

Flowable.zipIterable介绍

[英]Returns a Flowable that emits the results of a specified combiner function applied to combinations of items emitted, in sequence, by an Iterable of other Publishers.

zip applies this function in strict sequence, so the first item emitted by the new Publisher will be the result of the function applied to the first item emitted by each of the source Publishers; the second item emitted by the new Publisher will be the result of the function applied to the second item emitted by each of those Publishers; and so forth.

The resulting Publisher returned from zip will invoke onNext as many times as the number of onNext invocations of the source Publisher that emits the fewest items.

The operator subscribes to its sources in the order they are specified and completes eagerly if one of the sources is shorter than the rest while canceling the other sources. Therefore, it is possible those other sources will never be able to run to completion (and thus not calling doOnComplete()). This can also happen if the sources are exactly the same length; if source A completes and B has been consumed and is about to complete, the operator detects A won't be sending further values and it will cancel B immediately. For example:

zip(Arrays.asList(range(1, 5).doOnComplete(action1), range(6, 5).doOnComplete(action2)), (a) -> a)

action1 will be called but action2 won't.
To work around this termination property, use #doOnCancel(Action) as well or use using() to do cleanup in case of completion or cancellation.

Backpressure: The operator expects backpressure from the sources and honors backpressure from the downstream. (I.e., zipping with #interval(long,TimeUnit) may result in MissingBackpressureException, use one of the onBackpressureX to handle similar, backpressure-ignoring sources. Scheduler: zipIterable does not operate by default on a particular Scheduler.
[中]返回一个FlowTable,该FlowTable发出指定组合器函数的结果,该函数应用于由其他发布者的Iterable按顺序发出的项目组合。
zip严格按照顺序应用此函数,因此新发布者发出的第一项将是应用于每个源发布者发出的第一项的函数的结果;新发布者发出的第二项将是应用于每个发布者发出的第二项的函数的结果;等等
从zip返回的结果发布服务器调用onNext的次数与发出最少项的源发布服务器的onNext调用次数相同。
操作员按照指定的顺序订阅其源,如果其中一个源比其他源短,则急切地完成订阅,同时取消其他源。因此,这些其他源可能永远无法运行到完成(因此不调用doOnComplete()。如果源的长度完全相同,也可能发生这种情况;如果源A已完成且B已消耗且即将完成,则操作员检测到A将不会发送更多值,并将立即取消B。例如:

zip(Arrays.asList(range(1, 5).doOnComplete(action1), range(6, 5).doOnComplete(action2)), (a) -> a)

将调用action1,但不会调用action2。
要解决此终止属性,请同时使用#doOnCancel(操作)或使用using()在完成或取消时进行清理。
背压:操作员期望来自源头的背压,并尊重来自下游的背压。(即,使用#间隔(长,时间单位)压缩可能会导致背压缺失异常,使用OnBackPressureEx之一处理类似的背压源。调度器:Zipitable默认情况下不会在特定调度器上运行。

代码示例

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

@Override
  public Publisher<? extends R> apply(List<Publisher<? extends T>> list) {
    return Flowable.zipIterable(list, zipper, false, Flowable.bufferSize());
  }
}

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

@Override
  public Publisher<? extends R> apply(List<Publisher<? extends T>> list) {
    return Flowable.zipIterable(list, zipper, false, Flowable.bufferSize());
  }
}

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

@Test(expected = NullPointerException.class)
public void zipIterable2Null() {
  Flowable.zipIterable((Iterable<Publisher<Object>>)null, new Function<Object[], Object>() {
    @Override
    public Object apply(Object[] a) {
      return 1;
    }
  }, true, 128);
}

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

@SuppressWarnings("unchecked")
@Test(expected = NullPointerException.class)
public void zipIterable2FunctionNull() {
  Flowable.zipIterable(Arrays.asList(just1, just1), null, true, 128);
}

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

@SuppressWarnings("unchecked")
@Test(expected = NullPointerException.class)
public void zipIterable2FunctionReturnsNull() {
  Flowable.zipIterable(Arrays.asList(just1, just1), new Function<Object[], Object>() {
    @Override
    public Object apply(Object[] a) {
      return null;
    }
  }, true, 128).blockingLast();
}

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

@Test(expected = NullPointerException.class)
public void zipIterable2IteratorNull() {
  Flowable.zipIterable(new Iterable<Publisher<Object>>() {
    @Override
    public Iterator<Publisher<Object>> iterator() {
      return null;
    }
  }, new Function<Object[], Object>() {
    @Override
    public Object apply(Object[] a) {
      return 1;
    }
  }, true, 128).blockingLast();
}

相关文章

Flowable类方法