0

Problem Description

I have a simple class with simple function buildUseCaseObservable. Function should do following

  1. Try to get data from Internet first
    • If succeed write data to Database
    • If failed with exceptions like: ServerUnavailable, SocketException should read data from database and also should report error.
    • If receive data, but data was null or empty, should read data from database.

Basically everything works like expected, except bold point. In case something happens while getting data from web, only onError is called and onNext is not called.

I found out that there is method called onErrorResumeNext which is basically doing what I need but in that case I'm loosing error (onError is not called, instead onNext is called)

Question

Is the such method like onCompleteWithError or if no how to implement such a thing to not lose error?

@PerActivity public class DataInteractor extends Interactor { private RestService rest; private DataService data; @Inject AuthorsInteractor(RestService rest, DataService data) { this.rest = rest; this.data = data; } @Override protected Observable buildUseCaseObservable() { return Observable.concat( rest.getData().doOnNext(data -> data.setAuthors(authors)), data.getData()) .first(data -> data != null && !authors.isEmpty()); } } 

1 Answer 1

2

Unfortunately, in your case it seems like onError() and onNext() will be mutually exclusive. What I'd go with is a class that will allow you to return both the result and the error:

public static class Result<T> { T data; Throwable error; } 

then onErrorResumeNext() would look like:

.onErrorResumeNext(throwable -> new Result(data.getData(), throwable)) 
Sign up to request clarification or add additional context in comments.

3 Comments

and is there some more elegant way of doing what I want to do ?
This blog post may be useful for you @VictorApoyan. Cheers.
@YeMinHtut thanks it is useful, but not for my question.