gpt4 book ai didi

java - RxJava/Retrofit2/Java - NetworkBoundResource 未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:50 24 4
gpt4 key购买 nike

我遇到了一些有线问题。当我第一次订阅时,它会进行网络调用并将数据保存到数据库,但是 loadFromDb() 永远不会执行,并且不会引发任何错误。

为什么会发生这种情况?

    Flowable<Resource<List<List<DataSource>>>> getBoundResource(List<String> parentId) {
return new RxNetworkBoundResource<List<List<DataSource>>,
ContainerResponse>() {
@Override
void saveCallResult(@NonNull List<ContainerResponse> data) {
for (ContainerResponse item : data) {
// Saves data to database
List<DataSource> items = item.items;
containerDao.insert(items);
}
}

@Override
protected Flowable<List<List<DataSource>>> loadFromDb() {
return Flowable.just(parentId).flatMapIterable(d -> d)
.flatMap(s -> containerDao.loadContainerByParentIdRx(s))
.distinct()
.doOnNext(data -> {
// I am able to get data here
})
.toList() // I'm not able to get data after toList()
.toFlowable()
.doOnNext(data -> {
// Nothing here
});
}

@Override
protected Flowable<List<Response<ContainerResponse>>> createCall() {
String baseUrl =
MyApp.getApplication().getSharedConfig().getBaseUrl();
return Flowable.just(parentId).flatMapIterable(data -> data).flatMap(s -> {
String url = baseUrl + "?limit=30&offset=0&parent=" + s;
return Flowable.zip(Flowable.just(s),webservice.getContainersBoundRx(url),
(s1, response) -> {
if (response.body() == null) {
return response;
}
for (DataSource container : response.body().items) {
container.parentId = s1;
}
return response;
}).toList().toFlowable();
});
}

@Override
protected boolean shouldFetch() {
return false;
}
}.asFlowable();

subscribe()之后我无法得到任何东西。

    containerRepo.getBoundResource(parentId)
.subscribe(new Subscriber<Resource<List<List<DataSource>>>>() {
@Override
public void onSubscribe(Subscription s) {

}

@Override
public void onNext(Resource<List<List<DataSource>>> listResource) {
// No data
}

@Override
public void onError(Throwable t) {

}

@Override
public void onComplete() {
// This is never called
}
});

NetworkboundResource 类:

public abstract class RxNetworkBoundResource<ResultType, RequestType> {

private final String TAG = RxNetworkBoundResource.class.getSimpleName();

private Flowable<Resource<ResultType>> result;

RxNetworkBoundResource() {
// Lazy db observable.
Flowable<ResultType> dbObservable =
Flowable.defer(() -> loadFromDb().subscribeOn(Schedulers.computation()));

// Lazy network observable.
Flowable<ResultType> networkObservable = Flowable.defer(() ->
createCall()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.doOnNext(request -> {
if (request.get(0).isSuccessful()) {
saveCallResult(processResponse(request));
} else {
processInternalError(request);
}
})
.onErrorReturn(throwable -> {
throw Exceptions.propagate(throwable);
})
.flatMap(__ -> loadFromDb())
);

result = shouldFetch()
? networkObservable
.map(Resource::success)
.onErrorReturn(t -> Resource.error(t.getMessage(), null))
.observeOn(AndroidSchedulers.mainThread())
: dbObservable
.map(Resource::success)
.onErrorReturn(t -> Resource.error(t.getMessage(), null))
.observeOn(AndroidSchedulers.mainThread())
;
}

Flowable<Resource<ResultType>> asFlowable() {
return result;
}

private List<RequestType> processResponse(List<Response<RequestType>> response) {
List<RequestType> list = new ArrayList<>();
for (Response<RequestType> data : response) {
list.add(data.body());
}
return list;
}

private void processInternalError(List<Response<RequestType>> response) throws java.io.IOException {
for (Response<RequestType> data : response) {
if (data.errorBody() != null) {
String error = data.errorBody().string();
throw Exceptions.propagate(new Throwable(data.code() + ": " + error));
}
}
}

abstract void saveCallResult(@NonNull List<RequestType> item);

abstract Flowable<ResultType> loadFromDb();

abstract Flowable<List<Response<RequestType>>> createCall();

abstract boolean shouldFetch();

}

最佳答案

请注意,.toList() 仅在其上游完成后才会发出。 Doc

这里的问题很可能是因为这段代码返回了一个未完成的Flowable:

containerDao.loadContainerByParentIdRx(s)

如果此 Flowable 从未完成,则生成的 flatMap 也将不会完成,并且 toList() 不会发出任何内容。

如果您只查找数据库一次,那么一种选择是将返回类型更改为 SingleMaybe。例如,如果您切换到也许,您可以执行以下操作:

    @Override
protected Flowable<List<List<DataSource>>> loadFromDb() {
return Flowable.just(parentId).flatMapIterable(d -> d)
.flatMapMaybe(s -> containerDao.loadContainerByParentIdRx(s))
.distinct()
.doOnNext(data -> {
// I am able to get data here
})
.toList() // You should now get this as well.
.toFlowable()
.doOnNext(data -> {
// Nothing here
});
}

关于java - RxJava/Retrofit2/Java - NetworkBoundResource 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com