Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 15, 2020 00:13
Show Gist options
  • Save jasdev/8cf894fd9d62d096038ee0a0289f7972 to your computer and use it in GitHub Desktop.
Save jasdev/8cf894fd9d62d096038ee0a0289f7972 to your computer and use it in GitHub Desktop.
Subscribing to a `Subject`, post-completion.
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