ReactiveCocoa可以通过调用-subscribeCompleted:
将信号转换为“热”信号。但是我认为如果您不关心结果(即没有订阅者),则此方法非常冗长。
RACDisposable *animationDisposable = [[self play:animation] subscribeCompleted:^{
// just to make the animation play
}];
而且这三行内容不足以表达我的意图。
是否有用于类似目的的方法?谢谢!
最佳答案
"You keep using that word. I do not think it means what you think it means."
“热信号” 是一种发送值(并且假定确实起作用)的信号,而不管其是否有任何订阅者。 “冷信号” 是一个信号,它延迟其工作以及发送任何值,直到有订阅者为止。冷信号将执行其工作并为每个订户发送值。
如果要使冷信号仅运行一次但有多个订阅者,则需要多播信号。组播是一个非常简单的概念,其工作原理如下:
[subject sendNext:value]
将其发送给主题。 但是,您可以并且应该使用
RACMulticastConnection
以更少的代码完成上述所有操作: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`.