0Pricing
Swift Academy · Lesson

Custom Publishers and Subscribers

Build your own Combine components.

Beyond Built-in Publishers

Combine ships with many publishers, but sometimes you need to wrap a non-Combine API as a publisher, or consume values with precise backpressure control. That means implementing the Publisher and Subscriber protocols yourself.

The Publisher Protocol

A Publisher declares its Output and Failure types and implements receive(subscriber:), where it hands a subscription to the incoming subscriber.

protocol Publisher {
    associatedtype Output
    associatedtype Failure: Error
    func receive<S: Subscriber>(subscriber: S)
        where S.Input == Output, S.Failure == Failure
}

All lessons in this course

  1. Transforming and Combining Streams
  2. Schedulers and Threading
  3. Error Handling Operators
  4. Custom Publishers and Subscribers
← Back to Swift Academy