.zip 方法中的 Observable 数量是否有限制?
Is There A Limit to The Number of Observables in .zip Method?
在 Kotlin 的 zip 方法中用作参数的 Observable 数量似乎有限制。如果这是准确的,最好的选择是什么?
例如,当我使用 9 个参数时,它按预期工作。当我添加第 10 个参数时,我收到错误消息 无法推断此参数的类型。请明确指定
Observable.zip(
//TODO: parameterize exchange symbols based on pair
methodOne() as Observable<Any>),
methodTwo() as Observable<Any>),
methodThree() as Observable<Any>),
methodFour() as Observable<Any>),
methodFive() as Observable<Any>),
methodSix() as Observable<Any>),
methodSeven() as Observable<Any>),
methodEight() as Observable<Any>),
methodNine() as Observable<Any>),
{ oneResult, twoResult, threeResult, fourResult, fiveResult, sixResult, sevenResult, eightResult, nineResult ->
//logic here applying computation to results
})
.subscribe(
{},
{
println(String.format("Error: %s", it.message))
})
.unsubscribe()
}
是的,有几个重载方法 Observable.zip()
并且 ObservableSource
参数的最大数量是 9:
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Observable<R> zip(
ObservableSource<? extends T1> source1, ObservableSource<? extends T2> source2, ObservableSource<? extends T3> source3,
ObservableSource<? extends T4> source4, ObservableSource<? extends T5> source5, ObservableSource<? extends T6> source6,
ObservableSource<? extends T7> source7, ObservableSource<? extends T8> source8, ObservableSource<? extends T9> source9,
Function9<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9, ? extends R> zipper) {
如果您想拥有 9 个以上的来源,请查看 zipArray
or zipIterable
RxJava 仅支持最多 9 个具有 zip
的不同来源。除此之外,您必须使用 zip(Iterable<ObservableSource>, Func<Object[],R>)
方法并将 Object[]
的每个元素转换回其各自的类型。
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 ObservableSource
s.
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 ObservableSource
s; 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 ObservableSource
s; and so forth.
The resulting ObservableSource<R>
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<Integer[], R>
passed to the method would trigger a ClassCastException
.
根据 Oracle Java 文档:http://reactivex.io/RxJava/javadoc/rx/Observable.html#zip(java.lang.Iterable,%20rx.functions.FuncN)
存在这些方法:
static <R> Observable<R> zip(java.lang.Iterable<? extends Observable<?>> ws, FuncN<? extends R> zipFunction)
static <R> Observable<R> zip(Observable<?>[] ws, FuncN<? extends R> zipFunction)
static <R> Observable<R> zip(Observable <? extends Observable<?>> ws, FuncN<? extends R> zipFunction)
static <T1,T2,R> Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Func2<? super T1,? super T2,? extends R> zipFunction)
static <T1,T2,T3,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Func3<? super T1,? super T2,? super T3,? extends R> zipFunction)
static <T1,T2,T3,T4,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Func4<? super T1,? super T2,? super T3,? super T4,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Func5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Func6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Func7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,T8,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8, Func8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,T8,T9,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8, Observable<? extends T9> o9, Func9<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? super T9,? extends R> zipFunction)
因此,如果元素超过 9 个,则使用数组或可迭代对象会更简单
在 Kotlin 的 zip 方法中用作参数的 Observable 数量似乎有限制。如果这是准确的,最好的选择是什么?
例如,当我使用 9 个参数时,它按预期工作。当我添加第 10 个参数时,我收到错误消息 无法推断此参数的类型。请明确指定
Observable.zip(
//TODO: parameterize exchange symbols based on pair
methodOne() as Observable<Any>),
methodTwo() as Observable<Any>),
methodThree() as Observable<Any>),
methodFour() as Observable<Any>),
methodFive() as Observable<Any>),
methodSix() as Observable<Any>),
methodSeven() as Observable<Any>),
methodEight() as Observable<Any>),
methodNine() as Observable<Any>),
{ oneResult, twoResult, threeResult, fourResult, fiveResult, sixResult, sevenResult, eightResult, nineResult ->
//logic here applying computation to results
})
.subscribe(
{},
{
println(String.format("Error: %s", it.message))
})
.unsubscribe()
}
是的,有几个重载方法 Observable.zip()
并且 ObservableSource
参数的最大数量是 9:
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Observable<R> zip(
ObservableSource<? extends T1> source1, ObservableSource<? extends T2> source2, ObservableSource<? extends T3> source3,
ObservableSource<? extends T4> source4, ObservableSource<? extends T5> source5, ObservableSource<? extends T6> source6,
ObservableSource<? extends T7> source7, ObservableSource<? extends T8> source8, ObservableSource<? extends T9> source9,
Function9<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9, ? extends R> zipper) {
如果您想拥有 9 个以上的来源,请查看 zipArray
or zipIterable
RxJava 仅支持最多 9 个具有 zip
的不同来源。除此之外,您必须使用 zip(Iterable<ObservableSource>, Func<Object[],R>)
方法并将 Object[]
的每个元素转换回其各自的类型。
Returns an
Observable
that emits the results of a specifiedcombiner
function applied to combinations of items emitted, in sequence, by anIterable
of otherObservableSource
s. zip applies this function in strict sequence, so the first item emitted by the newObservableSource
will be the result of the function applied to the first item emitted by each of the sourceObservableSource
s; the second item emitted by the newObservableSource
will be the result of the function applied to the second item emitted by each of thoseObservableSource
s; and so forth.The resulting
ObservableSource<R>
returned from zip will invokeonNext
as many times as the number ofonNext
invocations of the sourceObservableSource
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 butaction2
won't. To work around this termination property, usedoOnDispose(Action)
as well or useusing()
to do cleanup in case of completion or adispose()
call. Note on method signature: since Java doesn't allow creating a generic array with newT[]
, the implementation of this operator has to create anObject[]
instead. Unfortunately, aFunction<Integer[], R>
passed to the method would trigger aClassCastException
.
根据 Oracle Java 文档:http://reactivex.io/RxJava/javadoc/rx/Observable.html#zip(java.lang.Iterable,%20rx.functions.FuncN)
存在这些方法:
static <R> Observable<R> zip(java.lang.Iterable<? extends Observable<?>> ws, FuncN<? extends R> zipFunction)
static <R> Observable<R> zip(Observable<?>[] ws, FuncN<? extends R> zipFunction)
static <R> Observable<R> zip(Observable <? extends Observable<?>> ws, FuncN<? extends R> zipFunction)
static <T1,T2,R> Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Func2<? super T1,? super T2,? extends R> zipFunction)
static <T1,T2,T3,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Func3<? super T1,? super T2,? super T3,? extends R> zipFunction)
static <T1,T2,T3,T4,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Func4<? super T1,? super T2,? super T3,? super T4,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Func5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Func6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Func7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,T8,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8, Func8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> zipFunction)
static <T1,T2,T3,T4,T5,T6,T7,T8,T9,R>
Observable<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8, Observable<? extends T9> o9, Func9<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? super T9,? extends R> zipFunction)
因此,如果元素超过 9 个,则使用数组或可迭代对象会更简单