Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava Error Handling

RxJava Error Handling

Bobby Prabowo

December 14, 2018
Tweet

More Decks by Bobby Prabowo

Other Decks in Programming

Transcript

  1. Why it matters? Usage Example 1. UI subscribe Event emitted

    from the ViewModel using the Observable 2. ViewModel receive an event/data from UseCase that subscribe to an Repository Event Effect 1. Any Event from the ViewModel will not be received by the UI 2. Any Side Effect event will from Repository will not work as intended
  2. Goals ViewModel should maintain the Event/State Stream alive even when

    error happen UI ViewModel ViewModel Repository Repository
  3. OnErrorReturn The input is a function that return a value

    public final Observable<T> onErrorReturn(Function<? super Throwable, ? extends T> valueSupplier)
  4. OnErrorReturnItem The input is the replacement value for the error

    public final Observable<T> onErrorReturnItem(final T item)
  5. OnErrorResumeNext The input is an Observable that will be the

    replacement value • public final Observable<T> onErrorResumeNext(Function<? super Throwable, ? extends ObservableSource<? extends T>> resumeFunction) • public final Observable<T> onErrorResumeNext(final ObservableSource<? extends T> next)
  6. Tips to keep the Stream alive Wrap that Observable inside

    another Observable/Flowable creation operator and use the error operator properly • Kotlin user: We can use the sealed class to wrap a Event
  7. Notes 1. Always expect the Error will emitted 2. Always

    override the subscriber onError 3. Use the onComplete to detect an Stream is closed, put log inside it source: https://github.com/bopbi/rxerror