Publisher
protocol · iOS 13.0+ · SDK 27.1
protocol Publisher<Output, Failure>Declares that a type can transmit a sequence of values over time. A publisher delivers elements to one or more ``Subscriber`` instances. The subscriber’s ``Subscriber/Input`` and ``Subscriber/Failure`` associated types must match the ``Publisher/Output`` and ``Publisher/Failure`` types declared by the publisher. The publisher implements the ``Publisher/receive(subscriber:)``method to accept a subscriber. After this, the publisher can call the following methods on the subscriber: - ``Subscriber/receive(subscription:)``: Acknowledges the subscribe request and returns a ``Subscription`` instance. The subscriber uses the subscription to demand elements from the publisher and can use it to cancel publishing. - ``Subscriber/receive(_:)``: Delivers one element from the publisher to the subscriber. - ``Subscriber/receive(completion:)``: Informs the subscriber that publishing has ended, either normally or with an error. Every `Publisher` must adhere to this contract for downstream subscribers to function correctly. Extensions on `Publisher` define a wide variety of _operators_ that you compose to create sophisticated event-processing chains. Each operator returns a type that implements the ``Publisher`` protocol Most of these types exist as extensions on the ``Publishers`` enumeration. For example, the ``Publisher/map(_:)-99evh`` operator returns an instance of ``Publishers/Map``. > Tip: A Combine publisher fills a role similar to, but distinct from, the <doc://com.apple.documentation/documentation/Swift/AsyncSequence> in the Swift standard library. A `Publisher` and an `AsyncSequence` both produce elements over time. However, the pull model in Combine uses a ``Combine/Subscriber`` to request elements from a publisher, while Swift concurrency uses the `for`-`await`-`in` syntax to iterate over elements published by an `AsyncSequence`. Both APIs offer methods to modify the sequence by mapping or filtering elements, while only Combine provides time-based operations like ``Publisher/debounce(for:scheduler:options:)`` and ``Publisher/throttle(for:scheduler:latest:)``, and combining operations like ``Publisher/merge(with:)-7fk3a`` and ``Publisher/combineLatest(_:_:)-1n30g``. To bridge the two approaches, the property ``Publisher/values-1dm9r`` exposes a publisher's elements as an `AsyncSequence`, allowing you to iterate over them with `for`-`await`-`in` rather than attaching a ``Subscriber``. # Creating Your Own Publishers Rather than implementing the `Publisher` protocol yourself, you can create your own publisher by using one of several types provided by the Combine framework: - Use a concrete subclass of ``Subject``, such as ``PassthroughSubject``, to publish values on-demand by calling its ``Subject/send(_:)`` method. - Use a ``CurrentValueSubject`` to publish whenever you update the subject’s underlying value. - Add the `@Published` annotation to a property of one of your own types. In doing so, the property gains a publisher that emits an event whenever the property’s value changes. See the ``Published`` type for an example of this approach.
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
118 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
Publisher.multicast(_:)
This declaration needs Swift generic type arguments. (no canonical witness for S)
Publisher.multicast(subject:)
This declaration needs Swift generic type arguments. (no canonical witness for S)
Publisher.subscribe(on:options:)
This declaration needs Swift generic type arguments. (no canonical witness for S)
Publisher.measureInterval(using:options:)
This declaration needs Swift generic type arguments. (no canonical witness for S)
Publisher.drop(while:)
callback cannot propagate synchronous failure
Publisher.tryDrop(while:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.filter(_:)
callback cannot propagate synchronous failure
Publisher.tryFilter(_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.breakpointOnError()
This protocol needs associated types that cannot yet cross the bridge.
Publisher.allSatisfy(_:)
callback cannot propagate synchronous failure
Publisher.tryAllSatisfy(_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.sink(receiveCompletion:receiveValue:)
The iOS SDK compiler rejected this binding: cannot access associated type 'Failure' from 'Publisher'; use a concrete type or generic parameter base instead
Publisher.sink(receiveValue:)
The iOS SDK compiler rejected this binding: cannot access associated type 'Output' from 'Publisher'; use a concrete type or generic parameter base instead
Publisher.removeDuplicates()
This protocol needs associated types that cannot yet cross the bridge.
Publisher.removeDuplicates(by:)
callback cannot propagate synchronous failure
Publisher.tryRemoveDuplicates(by:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.decode(type:decoder:)
Passing a Swift type itself is not supported.
Publisher.encode(encoder:)
This declaration needs Swift generic type arguments. (no canonical witness for Coder)
Publisher.contains(_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.combineLatest(_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.combineLatest(_:_:)
callback cannot propagate synchronous failure
Publisher.combineLatest(_:_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.combineLatest(_:_:_:)
callback cannot propagate synchronous failure
Publisher.combineLatest(_:_:_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.combineLatest(_:_:_:_:)
callback cannot propagate synchronous failure
Publisher.prefix(_:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.print(_:to:)
This protocol needs associated types that cannot yet cross the bridge.
Publisher.prefix(while:)
callback cannot propagate synchronous failure
Publisher.tryPrefix(while:)
This protocol needs associated types that cannot yet cross the bridge.