Retrofit 2 和带有 Flowable concat 的 rxJava 处理多个响应
Retrofit 2 and rxJava with Flowable concat handle several response
我正在使用 retrofit、rxJava 和 realm 我正在使用 flowable concat 向服务器发起一个请求,另一个用于 realm
这就是我制作一次性用品的方式:
disposable.add(launchRequest()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Config>() {
@Override
public void accept(Config config) throws Exception {
ProfileManager.getInstance().setConfig(config);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
if (!throwable.getMessage().equals(ErrorUtils.ERROR_CODE_304)) {
throwable.printStackTrace();
}
}
}));
launchRequest 函数创建 Flowable :
Flowable.concat(cloudDataStore.getById(single, diskDataStore, repositoryItem).toFlowable(), diskDataStore.getById(id, repositoryItem).toFlowable())
.map(new Function<Object, T>() {
@Override
public T apply(Object o) throws Exception {
return repositoryItem.toView((T) o);
}
});
cloudDataStore 参数是改装部分,diskDataStore 是领域部分。
这里一切正常,我的麻烦是当我处理改造请求时:
return single.flatMap(new Function<Response<T1>, SingleSource<T1>>() {
@Override
public SingleSource<T1> apply(@NonNull Response<T1> response) throws Exception {
if (response.code() == Integer.valueOf(ErrorUtils.CODE_200)) {
return Single.just(response.body());
} else if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Single.just(response.body());
} else {
return Single.error(new Throwable(String.valueOf(response.code())));
}
}
});
如果请求成功(状态 200)我 return 服务器对我的一次性响应。
如果我得到代码 304,则响应正文为 null,因此一次性 throwable 被触发,但如果 throwable 被触发,则一次性不要等待来自 concat 的领域响应并停止监听。
我找到的解决方法是创建一个空对象,然后 return 像这样:
if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Single.just(new Config());
}
这会触发具有空对象的一次性消费者,并且我可以在获得具有良好价值的领域结果后因为 throwable 未被触发。
但我不想收到这个空结果,我对此无能为力,我需要所有请求检查内容是否不为空,如下所示:
.subscribe(new Consumer<Config>() {
@Override
public void accept(Config config) throws Exception {
if (config.getContent != null){
ProfileManager.getInstance().setConfig(config);
}
}
}
我怎样才能 return 使用 Single 的东西不会触发消费者和可抛出的东西?
感谢 EpicPandaForce,解决方案是将 Single 更改为 Maybe,如下所示:
.flatMap(new Function<Response<T>, Maybe<T>>() {
@Override
public Maybe<T> apply(@NonNull Response<T> response) throws Exception {
if (response.code() == Integer.valueOf(ErrorUtils.CODE_200)) {
return Maybe.just(response.body());
} else if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Maybe.empty();
} else {
return Maybe.error(new Throwable(String.valueOf(response.code())));
}
}
});
我正在使用 retrofit、rxJava 和 realm 我正在使用 flowable concat 向服务器发起一个请求,另一个用于 realm
这就是我制作一次性用品的方式:
disposable.add(launchRequest()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Config>() {
@Override
public void accept(Config config) throws Exception {
ProfileManager.getInstance().setConfig(config);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
if (!throwable.getMessage().equals(ErrorUtils.ERROR_CODE_304)) {
throwable.printStackTrace();
}
}
}));
launchRequest 函数创建 Flowable :
Flowable.concat(cloudDataStore.getById(single, diskDataStore, repositoryItem).toFlowable(), diskDataStore.getById(id, repositoryItem).toFlowable())
.map(new Function<Object, T>() {
@Override
public T apply(Object o) throws Exception {
return repositoryItem.toView((T) o);
}
});
cloudDataStore 参数是改装部分,diskDataStore 是领域部分。 这里一切正常,我的麻烦是当我处理改造请求时:
return single.flatMap(new Function<Response<T1>, SingleSource<T1>>() {
@Override
public SingleSource<T1> apply(@NonNull Response<T1> response) throws Exception {
if (response.code() == Integer.valueOf(ErrorUtils.CODE_200)) {
return Single.just(response.body());
} else if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Single.just(response.body());
} else {
return Single.error(new Throwable(String.valueOf(response.code())));
}
}
});
如果请求成功(状态 200)我 return 服务器对我的一次性响应。
如果我得到代码 304,则响应正文为 null,因此一次性 throwable 被触发,但如果 throwable 被触发,则一次性不要等待来自 concat 的领域响应并停止监听。
我找到的解决方法是创建一个空对象,然后 return 像这样:
if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Single.just(new Config());
}
这会触发具有空对象的一次性消费者,并且我可以在获得具有良好价值的领域结果后因为 throwable 未被触发。
但我不想收到这个空结果,我对此无能为力,我需要所有请求检查内容是否不为空,如下所示:
.subscribe(new Consumer<Config>() {
@Override
public void accept(Config config) throws Exception {
if (config.getContent != null){
ProfileManager.getInstance().setConfig(config);
}
}
}
我怎样才能 return 使用 Single 的东西不会触发消费者和可抛出的东西?
感谢 EpicPandaForce,解决方案是将 Single 更改为 Maybe,如下所示:
.flatMap(new Function<Response<T>, Maybe<T>>() {
@Override
public Maybe<T> apply(@NonNull Response<T> response) throws Exception {
if (response.code() == Integer.valueOf(ErrorUtils.CODE_200)) {
return Maybe.just(response.body());
} else if (response.code() == Integer.valueOf(ErrorUtils.ERROR_CODE_304)) {
return Maybe.empty();
} else {
return Maybe.error(new Throwable(String.valueOf(response.code())));
}
}
});