8

I am try to use the following code

initLocalSettingsIfNeed()
                            .andThen(initGlobalSettingsIfNeed(configuration))
                            .doOnComplete(callback::onSuccess)
                            .doOnError(throwable -> callback.onError(throwable.getLocalizedMessage()))
                            .subscribe();

But I have exception

The exception was not handled due to missing onError handler in the subscribe() method call.

I guess I am not using this methods correctly, I thought can replace doOnComplete doOnError with observer inside subscribe() method, I am wrong?

Pavel Poley
  • 5,307
  • 4
  • 35
  • 66

2 Answers2

9

Regarding your original question, you have to know that doOnError is not a replacement of onError. You have a good and short explanation about it in this blog:

Actually there’s one key difference between them. doOnError() basically only triggers its callback, then passes down the encountered errors to the down stream. So if the whole stream is subscribed without the onError callback in subscribe(), your app will crash by OnErrorNotImplementedException.

The onError callback in subscribe() in the other hand does consume the errors. That means, it will catch the errors, and let you handle them without re-throwing the errors by itself.

About the warning you mention in one comment:

This approach is working, but i have warning 'the result of subscribe not used', as i know this need to be disposed automatically when onError or onComplete is called, is there way to avoid this warning? – Pavel Poley

A good approach is that your methods inside your Repository return a Observable, and then you can subscribe to them in your ViewModel. Then, in every ViewModel class you can have a member variable with a CompositeDisposable where you can add the disposable of each subscription to the Observables returned by your repository. Finally, you should override the onCleared method to dispose all the disposables stored in the CompositeDisposable.

public class MyViewModel extends ViewModel {

    private MyRepository myRepository;
    private final CompositeDisposable disposables;

    @Inject
    public MyViewModel(MyRepository myRepository) {
        ...
        this.myRepository = myRepository;
        disposables = new CompositeDisposable();
        ...
    }

    public void callObservableInRepository() {
         disposables.add(myRepository.myObservable()
                              .subscribe(onSuccess -> {...} , onError -> {...}));
    }

    @Override
    protected void onCleared() {
        disposables.clear();
    }

}
dglozano
  • 6,369
  • 2
  • 19
  • 38
3

try this

initLocalSettingsIfNeed()
    .andThen(initGlobalSettingsIfNeed(configuration))
    .subscribe({completed->
        callback.onSuccess()
    },{throwable->
        callback.onError(throwable.getLocalizedMessage())
    })
Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
  • This approach is working, but i have warning 'the result of subscribe not used', as i know this need to be disposed automatically when onError or onComplete is called, is there way to avoid this warning? – Pavel Poley Apr 16 '19 at 12:54
  • you can add it for example to CompositeDisposable() and dispose it after it is not needed anymore. Eg disposables.add(wrapped above code) and then disposables.dispose() – Antonis Radz Apr 16 '19 at 12:58
  • but i am not using it in activity, i am using it in repositories without lifecycle – Pavel Poley Apr 16 '19 at 13:00
  • Repostory usage logic should be handled in Model when using MVP or ViewModel when using MVVM – Antonis Radz Apr 16 '19 at 13:01
  • so it is not safe not to use result of disposable? event if it simple request onComplete onError? – Pavel Poley Apr 16 '19 at 13:08
  • in my case i using few repositories in top level component to encapsulate some async requests, so in this case i need to make the top level component lifecycle aware? – Pavel Poley Apr 16 '19 at 13:17
  • 1
    It depends on what kind of Observable are you using, if it is for example Single is completely safe to use it without disposing because it is emitting only one time. That what I am using for repository – Antonis Radz Apr 16 '19 at 13:22