I follow a pattern in my Rx code, I usually have an Observable trigger which I flatMap to create another Observable for a network request. A simplified example:
enum ViewModelError: Error {
case bang
}
enum DataTaskError: Error {
case bang
}
func viewModel(trigger: Observable<Void>,
dataTask: Observable<Result<SomeType, DataTaskError>>) -> Observable<Result<AnotherType, ViewModelError>> {
let apiResponse = trigger
.flatMap { dataTask }
}
The Combine equivalent I'm having some trouble with. I could use a Result as the Output type and use Never as the Failure type but that feels like a misuse of the API.
func viewModel(trigger: AnyPublisher<Void, Never>,
dataTask: AnyPublisher<SomeType, DataTaskError>) -> AnyPublisher<AnotherType, ViewModelError> {
let apiResponse = trigger
.flatMap { dataTask }
}
I get a compilation error:
Instance method 'flatMap(maxPublishers:_:)' requires the types 'Never' and 'DataTaskError' be equivalent
I could use mapError and cast both of the errors to Error, but I need a DataTaskError to be able to create my ViewModelError.
This feels like it shouldn't be so difficult, and it seems like a fairly common use case. I'm likely just misunderstanding some fundamentals, a point in the right direction would be greatly appreciated.
mapError
in your pipeline – FerreousError
and missed your mention ofmapError
. I wouldn't map toError
rather I would have a case ofViewModelError
that indicated there was an underlyingDataTaskError
. You thenmapError
fromDataTaskError
toViewModelError
. Your view model subscriber then only needs to know aboutViewModelError
– FerreousDataTaskError
yet, it's upset because theNever
Failure
type fromtrigger
doesn't match theFailure
type ofdataTask
. If that makes sense. – Weinshienk