本文整理汇总了Java中io.reactivex.Flowable.flatMap方法的典型用法代码示例。如果您正苦于以下问题:Java Flowable.flatMap方法的具体用法?Java Flowable.flatMap怎么用?Java Flowable.flatMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.reactivex.Flowable
的用法示例。
在下文中一共展示了Flowable.flatMap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<GankHttpResponse<T>, T> handleResult() { //compose判断结果
return new FlowableTransformer<GankHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<GankHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<GankHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(GankHttpResponse<T> tGankHttpResponse) {
if(!tGankHttpResponse.getError()) {
return createData(tGankHttpResponse.getResults());
} else {
return Flowable.error(new ApiException("服务器返回error"));
}
}
});
}
};
}
示例2: handleWXResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<WXHttpResponse<T>, T> handleWXResult() { //compose判断结果
return new FlowableTransformer<WXHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<WXHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<WXHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(WXHttpResponse<T> tWXHttpResponse) {
if(tWXHttpResponse.getCode() == 200) {
return createData(tWXHttpResponse.getNewslist());
} else {
return Flowable.error(new ApiException("服务器返回error"));
}
}
});
}
};
}
示例3: handleMyResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<MyHttpResponse<T>, T> handleMyResult() { //compose判断结果
return new FlowableTransformer<MyHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<MyHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<MyHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(MyHttpResponse<T> tMyHttpResponse) {
if(tMyHttpResponse.getCode() == 200) {
return createData(tMyHttpResponse.getData());
} else {
return Flowable.error(new ApiException("服务器返回error"));
}
}
});
}
};
}
示例4: handleGoldResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<GoldHttpResponse<T>, T> handleGoldResult() { //compose判断结果
return new FlowableTransformer<GoldHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<GoldHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<GoldHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(GoldHttpResponse<T> tGoldHttpResponse) {
if(tGoldHttpResponse.getResults() != null) {
return createData(tGoldHttpResponse.getResults());
} else {
return Flowable.error(new ApiException("服务器返回error"));
}
}
});
}
};
}
示例5: handleWXResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<WXHttpResponse<T>, T> handleWXResult() { //compose判断结果
return new FlowableTransformer<WXHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<WXHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<WXHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(WXHttpResponse<T> tWXHttpResponse) {
if(tWXHttpResponse.getCode() == 200) {
return createData(tWXHttpResponse.getNewslist());
} else {
return Flowable.error(new ApiException(tWXHttpResponse.getMsg(), tWXHttpResponse.getCode()));
}
}
});
}
};
}
示例6: handleMyResult
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 统一返回结果处理
* @param <T>
* @return
*/
public static <T> FlowableTransformer<MyHttpResponse<T>, T> handleMyResult() { //compose判断结果
return new FlowableTransformer<MyHttpResponse<T>, T>() {
@Override
public Flowable<T> apply(Flowable<MyHttpResponse<T>> httpResponseFlowable) {
return httpResponseFlowable.flatMap(new Function<MyHttpResponse<T>, Flowable<T>>() {
@Override
public Flowable<T> apply(MyHttpResponse<T> tMyHttpResponse) {
if(tMyHttpResponse.getCode() == 200) {
return createData(tMyHttpResponse.getData());
} else {
return Flowable.error(new ApiException(tMyHttpResponse.getMessage(), tMyHttpResponse.getCode()));
}
}
});
}
};
}
示例7: getApiTransformer
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* 异常处理变换
*
* @return
*/
public static <T extends IModel> FlowableTransformer<T, T> getApiTransformer() {
return new FlowableTransformer<T, T>() {
@Override
public Publisher<T> apply(Flowable<T> upstream) {
return upstream.flatMap(new Function<T, Publisher<T>>() {
@Override
public Publisher<T> apply(T model) throws Exception {
if (model == null || model.isNull()) {
return Flowable.error(new NetError(model.getErrorMsg(), NetError.NoDataError));
} else if (model.isAuthError()) {
return Flowable.error(new NetError(model.getErrorMsg(), NetError.AuthError));
} else if (model.isBizError()) {
return Flowable.error(new NetError(model.getErrorMsg(), NetError.BusinessError));
} else {
return Flowable.just(model);
}
}
});
}
};
}
示例8: cartesian
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
*
* @author [email protected]
* @param sources
* @return
*/
public static Flowable<int[]> cartesian(List<Flowable<Integer>> sources) {
if (sources.size() == 0) {
return Flowable.empty();
}
Flowable<int[]> main = Flowable.just(new int[0]);
for (int i = 0; i < sources.size(); i++) {
int j = i;
Flowable<Integer> o = sources.get(i).cache();
main = main.<int[]> flatMap(v -> o.map(w -> {
int[] arr = Arrays.copyOf(v, j + 1);
arr[j] = w;
return arr;
}));
}
return main;
}
示例9: apply
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Publisher<?> apply(@NonNull Flowable<Throwable> throwableFlowable) throws Exception {
return throwableFlowable
.flatMap(new Function<Throwable, Publisher<?>>() {
@Override
public Publisher<?> apply(@NonNull Throwable throwable) throws Exception {
if (++retryCount <= maxRetries) {
// When this Observable calls onNext, the original Observable will be retried (i.e. re-subscribed).
Log.d(TAG, "Flowable get error, it will try after " + retryDelaySecond
+ " second, retry count " + retryCount);
return Flowable.timer(retryDelaySecond,
TimeUnit.SECONDS);
}
// Max retries hit. Just pass the error along.
return Flowable.error(throwable);
}
});
}
示例10: getOne
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Flowable<Optional<T>> getOne(final Filter filter, final SortingMode sortingMode) {
List<Flowable<Optional<T>>> flowables = new ArrayList<>();
Flowable<Optional<T>> flowStorage = dao.getOne(filter, sortingMode);
if(hasSyncedStore()) {
flowStorage = flowStorage
.flatMap(new Function<Optional<T>, Flowable<Optional<T>>>() {
@Override
public Flowable<Optional<T>> apply(Optional<T> item) throws Exception {
return syncedStore.insertOrUpdate(item.get());
}
});
flowables.add(syncedStore.getOne(filter, sortingMode));
}
flowables.add(flowStorage);
return Flowable.concat(flowables);
}
示例11: getById
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Flowable<Optional<T>> getById(final int id) {
List<Flowable<Optional<T>>> flowables = new ArrayList<>();
Flowable<Optional<T>> flowStorage = dao.getById(id);
if(hasSyncedStore()) {
flowStorage = flowStorage
.flatMap(new Function<Optional<T>, Flowable<Optional<T>>>() {
@Override
public Flowable<Optional<T>> apply(final Optional<T> item) throws Exception {
return syncedStore.insertOrUpdate(item.get());
}
});
flowables.add(syncedStore.getById(id));
}
flowables.add(flowStorage);
return Flowable.concat(flowables);
}
示例12: apply
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Flowable<?> apply(Flowable<? extends Throwable> flowable) throws Exception {
return flowable.flatMap(new Function<Throwable, Publisher<?>>() {
@Override
public Publisher<?> apply(Throwable throwable) throws Exception {
try {
if (throwable.getMessage().equals("write failed: ENOSPC (No space left on device)")) {
//NetLogUtil.printLog("e", LOG_TAG, "磁盘空间不足,不发起重试请求", throwable);
return Flowable.error(throwable);
} else if (throwable instanceof IOException) {
retryCount += 1;
if (retryCount <= maxRetries) {
NetLogUtil.printLog("e", LOG_TAG, "第" + retryCount + "次失败重试,在" + retryDelayMillis + "毫秒后开始", throwable);
return Flowable.timer(retryDelayMillis, TimeUnit.MILLISECONDS, Schedulers.trampoline());
} else {
NetLogUtil.printLog("e", LOG_TAG, "重试次数已用尽", throwable);
return Flowable.error(throwable);
}
} else {
NetLogUtil.printLog("e", LOG_TAG, "不是IOException,即不是网络异常,不发起重试", throwable);
return Flowable.error(throwable);
}
} catch (Exception e) {
throw e;
}
}
});
}
示例13: apply
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Publisher<?> apply(Flowable<Throwable> throwableFlowable) throws Exception {
return throwableFlowable.flatMap(new Function<Throwable, Publisher<?>>() {
@Override
public Publisher<?> apply(Throwable throwable) throws Exception {
if(++retryCount <= maxRetries){
return todoBeforeRetry.apply(throwable).toFlowable(BackpressureStrategy.BUFFER);
}
return Flowable.error(throwable);
}
});
}
示例14: apply
import io.reactivex.Flowable; //导入方法依赖的package包/类
@Override
public Publisher<?> apply(Flowable<Throwable> throwableFlowable) throws Exception {
return throwableFlowable.flatMap(new Function<Throwable, Publisher<?>>() {
@Override
public Publisher<?> apply(Throwable throwable) throws Exception {
if (++retryCount <= maxRetries) {
Log.d(TAG, "get error, it will try after + retryDelaySecond" + " second, retry count + retryCount");
return Flowable.timer(retryDelaySecond, TimeUnit.SECONDS);
}
return Flowable.error(throwable);
}
});
}
示例15: wrapOptional
import io.reactivex.Flowable; //导入方法依赖的package包/类
/**
* Wrap a flowable object into an flowable optional
*/
protected <S> Flowable<Optional<S>> wrapOptional(Flowable<S> obj){
return obj.flatMap(new Function<S, Flowable<Optional<S>>>() {
@Override
public Flowable<Optional<S>> apply(S s) throws Exception {
return wrapOptional(s);
}
});
}