何时使用RACReplaySubject与RACMulticastConnection? [英] When to use RACReplaySubject vs. RACMulticastConnection?

查看:97
本文介绍了何时使用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:

重播主题保存其发送的值(直到其定义的容量) 并将其重新发送给新订户.它还会重播错误或 完成.

A replay subject saves the values it is sent (up to its defined capacity) and resends those to new subscribers. It will also replay an error or completion.

对于RACMulticastConnection:

And for RACMulticastConnection:

多播连接封装了共享一个订阅的想法. 向许多用户发出信号.如果订阅以下内容,这是最常需要的 潜在信号涉及副作用或不应被称为 一次.

A multicast connection encapsulates the idea of sharing one subscription to a signal to many subscribers. This is most often needed if the subscription to the underlying signal involves side-effects or shouldn't be called more than once.

仅在以下情况下订阅多播信号 -[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:

  • 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆