When to use RACReplaySubject vs. RACMulticastConnection?

别说谁变了你拦得住时间么 提交于 2019-12-02 16:49:24

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!