RxSwift Handle Error Gracefully?
Just working a little bit ‘again’ with project that uses RxSwift.
We’re facing an issue when the sequence is flatMapped with external source which it can emit error.
button.rx.tap
.flatMapLatest {
API.index()
}
.subscribe()
.disposed(by: bag)
The API.index()
has possibility to emit error.
Here’s what we’ve just found. If the inner API.index()
emit error, it will pass the event to main button.rx.tap
sequence which in turns dispose it.
But, if API.index()
is completed, the completion events will dispose inner Observable but will not be passed to the main sequence. Hmm, strange?
We need explanation. But for now, for every external sources that called to main sequence, we should end it in catchError
call.
.flatMapLatest {
API.index()
.catchError { _ in Observable.empty() }
}
So the external source will be disposed but not dispose the main sequence.
Conclusion
We need to read more.