Answer the question
In order to leave comments, you need to log in
RxJava. Why doesn't this code work?
Just started learning RxJava and trying to implement offline validation. When there is a network, it should issue from a retrofit, when offline - from the database.
Code from Repository:
public Observable<List<Category>> getCategories() {
return App.getInstance().isConnectionAvailable()
.filter(connection -> connection)
.switchMap(connection -> remoteDataSource.getCategories()
.doOnNext(categories -> localDataSource.insertCategories(categories)))
.switchIfEmpty(observer -> localDataSource.getCategories());
}
disposable.add(repository.getCategories()
.compose(RxUtils.async()) // subscribeOn.io/observeOn
.subscribe(view::showCategories, t -> ErrorHandler.handleError(view, t)));
Answer the question
In order to leave comments, you need to log in
I changed the architecture: I check the network when receiving data from remoteDataSource. If there is no network, I throw an error. In the main repository I do onErrorResumeNext , check for IOException and return data from localDataSource
I bet that isConnectionAvailable() returns an infinite Observable, so switchIfEmpty won't work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question