Combine: what are those multicast functions for?

There are three things to distinguish:

  • Subject

  • Multicast

  • Share

Subject is already a class so we get reference semantics. Thus a Subject is already a multicaster and there is no need to do anything else.

Multicast takes advantage of that fact; it takes as its parameter a function that produces a Subject and holds on to that, thus allowing us to multicast to any subscribers. It is a ConnectablePublisher so the upstream won't actually start publishing until you say connect.

Share is a convenience wrapper for Multicast; it appends autoconnect so the upstream starts publishing as soon as we are subscribed to.

I now have a free online tutorial with examples that may help clarify:

https://www.apeth.com/UnderstandingCombine/operators/operatorsSplitters/operatorssplitters.html

https://www.apeth.com/UnderstandingCombine/operators/operatorsSplitters/operatorsshare.html

https://www.apeth.com/UnderstandingCombine/operators/operatorsSplitters/operatorsmulticast.html

2 Likes