Applies a closure to create a subject that delivers elements to subscribers.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
A closure to create a new
Subjecteach time a subscriber attaches to the multicast publisher.
Use a multicast publisher when you have multiple downstream subscribers, but you want upstream publishers to only process one
receive(_:) call per event. This is useful when upstream publishers are doing expensive work you don’t want to duplicate, like performing network requests.
The following example uses a sequence publisher as a counter to publish three random numbers, generated by a
map(_:) operator. It uses a
multicast(_:) operator whose closure creates a
Passthrough to share the same random number to each of two subscribers. Because the multicast publisher is a
Connectable, publishing only begins after a call to
In this example, the output shows that the
print(_: operator receives each random value only one time, and then sends the value to both subscribers.