Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 29, 2020 23:30
Show Gist options
  • Save jasdev/fd93f909e39c360b1a89826c5250f135 to your computer and use it in GitHub Desktop.
Save jasdev/fd93f909e39c360b1a89826c5250f135 to your computer and use it in GitHub Desktop.
Manual retry with double subscribe.
enum SomeError: Error {
case anError
}
let subject = PassthroughSubject<Int, SomeError>()
let sink = Subscribers.Sink<Int, SomeError>(
receiveCompletion: { print($0) },
receiveValue: { print("Sink: \($0)") }
)
subject
.subscribe(sink) /// (1) Note, `Publisher.subscribe(_:)` (the `Subscriber` variant)
/// is `Void`-returning, so we don’t need to store away an `AnyCancellable`.
subject.send(1)
subject.send(2)
subject.send(3)
subject.send(completion: .failure(.anError))
subject
.subscribe(sink) /// (2) Mimicking `retry(1)` attempt to recreate the subscription.
subject.send(4)
/// (3) Output:
/// ```none
/// Sink: 1
/// Sink: 2
/// Sink: 3
/// failure(__lldb_expr_146.SomeError.anError)
/// failure(__lldb_expr_146.SomeError.anError)
/// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment