T
T
terminator-light2019-06-28 18:33:47
Java
terminator-light, 2019-06-28 18:33:47

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());
    }

with a network it works correctly, but without a network from the database it does not give out for some reason. But at the same time, the localDataSource.getCategories() method is called.
When I leave return localDataSource.getCategories() instead of the body of the above method, the data from the database comes and is displayed.
And the call in the presenter:
disposable.add(repository.getCategories()
                .compose(RxUtils.async()) // subscribeOn.io/observeOn
                .subscribe(view::showCategories, t -> ErrorHandler.handleError(view, t)));

With the first option, nothing gets into view::showCategories and ErrorHandler.handleError , they are not called.
What can be wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
terminator-light, 2019-06-29
@terminator-light

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

D
Denis Zagaevsky, 2019-06-28
@zagayevskiy

I bet that isConnectionAvailable() returns an infinite Observable, so switchIfEmpty won't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question