When to use RACReplaySubject vs. RACMulticastConnection?

前端 未结 1 766
执念已碎
执念已碎 2021-01-31 05:57

Using ReactiveCocoa, there seem to be two ways to have subscribers receive the same values from a signal, rather than re-triggering whatever operation generates those values: Vi

相关标签:
1条回答
  • 2021-01-31 06:12

    Actually, they're not mutually exclusive, and can even be used together.

    The main purpose of RACMulticastConnection is to subscribe to a base signal, and then multicast that subscription to any number of other subscribers, without triggering the base signal's side effects multiple times.

    RACMulticastConnection accomplishes this by sending values to a private RACSubject, which is exposed via the connection's signal property. Subscribers attach to the subject (which doesn't cause any side effects), and the connection forwards all of the base signal's events there.

    There are a few different methods to create a connection:

    • The -publish creates a connection with a plain RACSubject. This subject will not replay previous values to new subscribers.
    • The -multicast: method creates a connection with a subject of your choice. You could decide to use a RACReplaySubject here.
    • The -replay, -replayLast, and -replayLazily methods are conveniences for creating a connection with a RACReplaySubject, and then also automatically connecting to it.

    If in doubt, -replayLazily will probably do what you want, because it saves all values and only triggers any side effects (or starts any work) when the returned signal receives a subscription.

    0 讨论(0)
提交回复
热议问题