Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 1: Getting Started

02. Publishers and Subscribers

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Introduction Next episode: 03. Subscriber Operators and Subjects

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Now that you’ve learned some of the basic concepts of Combine, it’s time to jump in and learn about two of Combine’s core components: publishers and subscribers.

example(of: "NotificationCenter") {
  let center = NotificationCenter.default
  let myNotification = Notification.Name("MyNotification")

  let publisher = center
    .publisher(for: myNotification, object: nil)
}
let subscription = publisher
  .sink { _ in
    print("Notification received from a publisher!")
  }

center.post(name: myNotification, object: nil)
subscription.cancel()
——— Example of: Publisher ———
Notification received from a publisher!
.print()
——— Example of: NotificationCenter ———
receive subscription: (NotificationCenter Observer)
request unlimited
receive value: (name = MyNotification, object = nil, userInfo = nil)
Notification received from a publisher!
receive cancel