本文介绍了何时使用RACReplaySubject与RACMulticastConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ReactiveCocoa ,似乎有两种方法可以使订阅者从信号中接收相同的值,而不是重新触发任何操作来生成这些值:通过RACReplaySubject或RACMulticastConnection.

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: Via RACReplaySubject or RACMulticastConnection.

这是RACReplaySubject的头文件:

Here are the header docs for RACReplaySubject:

对于RACMulticastConnection:

And for RACMulticastConnection:

仅在以下情况下订阅多播信号 -[RACMulticastConnection connect]被调用.在此之前,没有任何价值 将在signal上发送.有关方法,请参见-[RACMulticastConnection autoconnect] -[RACMulticastConnection connect]可以自动调用.

The multicasted signal is only subscribed to when -[RACMulticastConnection connect] is called. Until that happens, no values will be sent on signal. See -[RACMulticastConnection autoconnect] for how -[RACMulticastConnection connect] can be called automatically.

请注意,您不应该手动创建RACMulticastConnection.改为使用 -[RACSignal publish]-[RACSignal multicast:].

Note that you shouldn't create RACMulticastConnection manually. Instead use -[RACSignal publish] or -[RACSignal multicast:].

有人可以提供有关何时使用RACReplaySubject或RACMulticastConnection的简单指南吗?

Can someone provide simple guidelines as to when you would use RACReplaySubject or RACMulticastConnection?

推荐答案

实际上,它们不是互斥的,甚至可以一起使用.

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

RACMulticastConnection 的主要目的是订阅基础信号,然后将该订阅组播到任意数量的其他订阅者,多次触发基本信号的副作用.

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通过将值发送到私有 RACSubject ,该属性通过连接的 signal 属性公开.订户附加到主题(不会引起任何副作用),并且连接将所有基本信号的事件转发到该主题.

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:

  • -publish 创建连接用普通的RACSubject.该主题不会将以前的值重播给新订户.
  • -multicast: 方法创建一个与您选择的主题的联系.您可以决定在此处使用RACReplaySubject.
  • -replay -replayLast 和方法很方便,可以与连接.
  • 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.

如有疑问,-replayLazily可能会做您想要的事情,因为它保存所有值,并且仅在返回的信号收到订阅时才触发任何副作用(或开始任何工作).

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.

这篇关于何时使用RACReplaySubject与RACMulticastConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:00