-
-
Save jasdev/8cf894fd9d62d096038ee0a0289f7972 to your computer and use it in GitHub Desktop.
Subscribing to a `Subject`, post-completion.
This file contains 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
var subscriptions = Set<AnyCancellable>() | |
let subject = CurrentValueSubject<Int, Never>(1) | |
subject | |
.sink( | |
receiveCompletion: { print("First completion: \($0)") }, | |
receiveValue: { print("First sink: \($0)") } | |
) | |
.store(in: &subscriptions) | |
subject.send(completion: .finished) /// (1) Making sure the finish event | |
/// beats out the second sink, below. | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) { | |
subject | |
.sink( | |
receiveCompletion: { print("Second completion: \($0)") }, | |
receiveValue: { print("Second sink: \($0)") } | |
) | |
.store(in: &subscriptions) | |
} | |
/// (2) Same output as the `cherReplayingLatest` example. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment