Last active
April 4, 2018 18:15
-
-
Save skoirala/4f543f864b8381593b1034abb660eeb3 to your computer and use it in GitHub Desktop.
Dealing with Error in RxSwift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Handling Error in RxSwift
Support we have the following code,
The method
performApiCall
can throw error and can fail.If success button is pressed for few times, it can be seen that successLabel correctly shows the changes with number of times clicked with successbutton. As soon as failure button is clicked, the
performApiCall
throws error and so, thesequence
completes at this time. Now, further pressing successButton or failureButton wont do anything, since the sequence already completed.How can this be fixed ? Such that we can catch the error and not let the sequence to complete ?
There are various ways to deal with this, listed as follows:
Using
catchErrorJustReturn(element:)
This method allows to catch the error from failing sequences and return any element.
Lets say we make change to the code a bit such that we can box the result. Lets define an enum
State
The gist here is to catch the error and return failed status such that sequence does not complete.
The modification to the code,
performApiCall(status:)
still remains unchanged except an small change such that it can acceptstatus
.Now, this will correctly reflect inside label.
Using
catchError(handler: (Error) throws -> Observable<E>)
This works very similar to the above
catchErrorJustReturn(element:)
, except it passes a closure which can return an Observable of element. Only small modification to the above code would be needed to use this method.Using
materialize()
anddematerialize()
:materialize()
wraps events in an Event. The returned Observable never errors, but it does complete after observing all of the events of the underlying Observable.dematerialize()
convert any previously materialized Observable into it’s original form.The original throwing
Observable
can be materialized and later dematerialized such that error is not thrown and sequence wont complete immediately.materliazed sequence have property
element
which is of type optional. The value of this property is nil if error is thrown, if property is not nil, we can be sure that no error was thrown. This can be used to not stop sequence from completing.Notice, how materialize helps in dealing with error. The filter used in filtering error also checks if
isCompleted
, since materializing sequence sends next and completes it immediately, which also haselement
property nil. So, completed events are not taken into account when dealing with errorThe example shows simple usecase. The status and error handling can be extended to bind the error to some variable or then box the error and completed sequences such that Observer can deal with the error sensibly.