Skip to content

Instantly share code, notes, and snippets.

@ramunasjurgilas
Last active February 27, 2020 12:30
Show Gist options
  • Save ramunasjurgilas/a24daa59c995fb5989e71436080b2a1b to your computer and use it in GitHub Desktop.
Save ramunasjurgilas/a24daa59c995fb5989e71436080b2a1b to your computer and use it in GitHub Desktop.
Combining operator: switchToLatest()
let intPublisher1 = PassthroughSubject<Int, Never>()
let intPublisher2 = PassthroughSubject<Int, Never>()
let publishers = PassthroughSubject<PassthroughSubject<Int, Never>, Never>()
publishers.switchToLatest().sink { print($0) }
publishers.send(intPublisher1)
intPublisher1.send(-3)
intPublisher1.send(-5)
intPublisher2.send(0) // Zero will not be published as publisher listening for latest publisher.
publishers.send(intPublisher2)
intPublisher2.send(100)
intPublisher2.send(200)
// Output:
// -3
// -5
// 100
// 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment