Given the following code:
enum MyError: Error {
case someError
}
myButton.publisher(for: .touchUpInside).tryMap({ _ in
if Bool.random() {
throw MyError.someError
} else {
return "we're in the else case"
}
})
.replaceError(with: "replaced Error")
.sink(receiveCompletion: { (completed) in
print(completed)
}, receiveValue: { (sadf) in
print(sadf)
}).store(in: &cancellables)
Whenever I tap the button, I get we're in the else case
until Bool.random()
is true - now an error is thrown. I tried different things, but I couldn't achieve to catch/replace/ignore the error and just continue after tapping the button.
In the code example I would love to have e.g. the following output
we're in the else case
we're in the else case
replaced Error
we're in the else case
...
instead I get finished
after the replaced error
and no events are emitted.
Edit
Given a publisher with AnyPublisher<String, Error>
, how can I transform it to a AnyPublisher<String, Never>
without completing when an error occurs, i.e. ignore errors emitted by the original publisher?