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

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

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

Observable.zipIterable介绍

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

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

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

The operator subscribes to its sources in order they are specified and completes eagerly if one of the sources is shorter than the rest while disposing 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 dispose 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 #doOnDispose(Action) as well or use using() to do cleanup in case of completion or a dispose() call.

Note on method signature: since Java doesn't allow creating a generic array with new T[], the implementation of this operator has to create an Object[] instead. Unfortunately, a Function passed to the method would trigger a ClassCastException.

Scheduler: zipIterable does not operate by default on a particular Scheduler.
[中]返回一个Observable,该Observable发出指定组合器函数的结果,该函数应用于由其他ObservableSource的Iterable按顺序发出的项的组合。
zip以严格的顺序应用此函数,因此新ObservableSource发出的第一项将是应用于每个源ObservableSource发出的第一项的函数的结果;新的可观测资源发出的第二项将是应用于每个可观测资源发出的第二项的函数的结果;等等
从zip返回的结果ObservableSource调用onNext的次数与发出最少项的源ObservableSource的onNext调用次数相同。
操作员按指定的顺序订阅其源,如果其中一个源比其他源短,则在处理其他源时急切地完成。因此,这些其他源可能永远无法运行到完成(因此不调用doOnComplete()。如果源的长度完全相同,也可能发生这种情况;如果源A完成且B已被消耗且即将完成,则操作员检测到A不会发送更多值,并将立即处理B。例如:

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

将调用action1,但不会调用action2。
要解决此终止属性,请同时使用#doOnDispose(Action)或使用using()在完成或dispose()调用时进行清理。
关于方法签名的注意事项:由于Java不允许使用新的t[]创建泛型数组,因此该操作符的实现必须创建一个对象[]。不幸的是,传递给该方法的函数将触发ClassCastException。
调度器:Zipitable默认情况下不会在特定的调度器上运行。

代码示例

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

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

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

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

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

@Test(expected = NullPointerException.class)
public void zipIterable2Null() {
  Observable.zipIterable((Iterable<Observable<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() {
  Observable.zipIterable(Arrays.asList(just1, just1), null, true, 128);
}

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

@SuppressWarnings("unchecked")
@Test(expected = NullPointerException.class)
public void zipIterable2FunctionReturnsNull() {
  Observable.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() {
  Observable.zipIterable(new Iterable<Observable<Object>>() {
    @Override
    public Iterator<Observable<Object>> iterator() {
      return null;
    }
  }, new Function<Object[], Object>() {
    @Override
    public Object apply(Object[] a) {
      return 1;
    }
  }, true, 128).blockingLast();
}

相关文章

Observable类方法