I have an observable (request from network) and dont want it to be disposed when I got an error
My custom error
enum MyError: Error {
case notFound
case unknown
}
My network request using Moya
let registerRequest = didTapJoinButton.withLatestFrom(text.asObservable())
.flatMapLatest { text in
provider.rx.request(API.register(text: text))
}
.flatMapLatest({ (response) -> Observable<Response> in
let statusCode = response.statusCode
if statusCode.isSuccessStatus() {
return Observable.just(response)
} else if statusCode.isNotFoundStatus() {
return Observable.error(MyError.notFound)
} else {
return Observable.error(MyError.unknown)
}
})
.materialize()
.share(replay: 1)
Looks great. I use materialize() to prevent observable being disposed on error
Subscribe: (If status code 200) Everything works just fine, I got response and the stream is not disposed
registerEventRequest.subscribe(onNext: { (next) in
print("NEXT: \(next)")
}, onError: { (error) in
print("ERRRRROR ME: \(error)")
}, onCompleted: {
print("Completed")
}) {
print("Disposed")
}
BUT if status code is something like 404. I got the error as I expected. However, hey look at the console log
NEXT: error(notFound)
Completed
Disposed
It jumps to NEXT which I expected. But why it throw complete and dispose my sequence.
My question is Why did it dispose my sequence and how I can prevent this?