Observe errors in PassthroughSubject in SwiftUI

Viewed 168

Let's say I have the following PassthroughSubject, somewhere in an ObservableObject:

@Published var errorOccured = PassthroughSubject<(), Error>()

I send an error to it with:

self.errorOccured.send(completion: .failure(error))

How can I be notified in a View whenever an error occurs? I know that with onReceive I can be notified of values, but I am interested in errors.

1 Answers

Keep in mind that you are emitting a completion event so you only get to emit an error once using the method you have outlined. If you were to handle the completion event within SwiftUI, your UI would be frozen afterwords. This is not likely what you want, and I don't think the library provides such a method. Consider using a PassthroughSubject<Error, Never> instead.

Related