-
-
Save jasdev/fd93f909e39c360b1a89826c5250f135 to your computer and use it in GitHub Desktop.
Manual retry with double subscribe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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