Skip to content

Instantly share code, notes, and snippets.

@jasdev

jasdev/manual_retry.swift Secret

Last active Apr 29, 2020
Embed
What would you like to do?
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