如何用多个期货组合一个 Single<T>
How to compose a Single<T> with multiple futures
假设我有以下代码:
public Single<C> gen(String input1, String input2) {
//TODO
}
private Future<A> remote1(final String input1) {
}
private Future<B> remote2(final String input2) {
}
并且我可以使用 A + B 创建 C,例如 new C(A a, B b)。
我应该如何使用 remote1 和 remote2 运行 并行编写代码。
我不确定我的方法是否正确或有效:
Flowable<A> f1 = Flowable.fromFuture(remote1(input1));
Flowable<B> f2 = Flowable.fromFuture(remote2(input2));
Single<C> result = Flowable.combineLatest(f1, f2, new BiFunction<A, B, C>(){
@Override
public C apply(A a, B b) throws Exception {
return new C(a, b);
}}).firstOrError();
谢谢
里昂
怎么样:
Single<A> f1 = Single.fromFuture(remote1(input1));
Single<B> f2 = Single.fromFuture(remote2(input2));
Single<C> result = Single.zip(f1, f2, new BiFunction<A, B, C>(){
@Override
public C apply(A a, B b) throws Exception {
return new C(a, b);
}
});
假设我有以下代码:
public Single<C> gen(String input1, String input2) {
//TODO
}
private Future<A> remote1(final String input1) {
}
private Future<B> remote2(final String input2) {
}
并且我可以使用 A + B 创建 C,例如 new C(A a, B b)。
我应该如何使用 remote1 和 remote2 运行 并行编写代码。
我不确定我的方法是否正确或有效:
Flowable<A> f1 = Flowable.fromFuture(remote1(input1));
Flowable<B> f2 = Flowable.fromFuture(remote2(input2));
Single<C> result = Flowable.combineLatest(f1, f2, new BiFunction<A, B, C>(){
@Override
public C apply(A a, B b) throws Exception {
return new C(a, b);
}}).firstOrError();
谢谢
里昂
怎么样:
Single<A> f1 = Single.fromFuture(remote1(input1));
Single<B> f2 = Single.fromFuture(remote2(input2));
Single<C> result = Single.zip(f1, f2, new BiFunction<A, B, C>(){
@Override
public C apply(A a, B b) throws Exception {
return new C(a, b);
}
});