如何使RACSignal变得炙手可热? [英] How to make RACSignal to become hot?

查看:107
本文介绍了如何使RACSignal变得炙手可热?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReactiveCocoa可以通过调用其-subscribeCompleted:将信号转换为热"信号.但是我认为,如果您不关心结果(即没有订阅者),则此方法非常冗长.

ReactiveCocoa can convert the signal to "hot" signal by calling its -subscribeCompleted:. But I think this method is quite verbose if you do not care about the result (i.e. no subscribers).

RACDisposable *animationDisposable = [[self play:animation] subscribeCompleted:^{
    // just to make the animation play
}];

这三行内容不足以表达我的意图.

And these 3 lines are not expressive enough to show my intention.

是否有用于类似目的的方法?谢谢!

Is there any method for similar purpose? Thanks!

推荐答案

除了使其变热(=使其运行一次)外,我什么也不想做.

I want to do nothing except making it hot (=make it run once).

>您一直在使用该词.我认为它并不意味着您认为的含义."

热信号" 是一种发送值的信号(假定确实有效),而不管它是否有任何订户. 冷信号" 是一种信号,它会延迟其工作并发送任何值,直到有订阅者为止.冷信号将执行其工作并为每个订户发送值.

A "hot signal" is a signal that sends values (and presumably does work) regardless of whether it has any subscribers. A "cold signal" is a signal that defers its work and the sending of any values until it has a subscriber. And a cold signal will perform its work and send values for each subscriber.

如果您要使冷信号只运行一次但有多个订阅者,则需要多播.组播是一个非常简单的概念,其工作原理如下:

If you want to make a cold signal run only once but have multiple subscribers, you need to multicast the signal. Multicasting is a pretty simple concept, that works like this:

  1. 创建一个RACSubject来代理您要执行一次的信号发送的值.
  2. 根据需要多次订阅主题.
  3. 为您只想执行一次的信号创建单个订阅,对于信号发送的每个值,请使用[subject sendNext:value]将其发送给主题.
  1. Create a RACSubject to proxy the values sent by the signal you want to execute once.
  2. Subscribe to the subject as many times as needed.
  3. Create a single subscription to the signal you want to execute only once, and for every value sent by the signal, send it to the subject with [subject sendNext:value].

但是,您可以并且应该使用RACMulticastConnection来用更少的代码来完成上述所有操作:

However, you can and should use RACMulticastConnection to do all of the above with less code:

RACMulticastConnection *connection = [signal publish];
[connection.signal subscribe:subscriberA];
[connection.signal subscribe:subscriberB];
[connection.signal subscribe:subscriberC];
[connection connect]; // This will cause the original signal to execute once.
                      // But each of subscriberA, subscriberB, and subscriberC
                      // will be sent the values from `signal`.

这篇关于如何使RACSignal变得炙手可热?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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