问题描述
我正在使用ReactiveCocoa,我有几个SignalProducers
I'm using ReactiveCocoa and I have several SignalProducers
let center = NSNotificationCenter.defaultCenter()
let signalProducer1 = center.rac_notification(name: notificationName1, object: nil)
let signalProducer2 = center.rac_notification(name: notificationName2, object: nil)
let signalProducer3 = center.rac_notification(name: notificationName3, object: nil)
我想将它们组合成一个信号产生器,只要其中一个产生信号就会产生信号一个信号。
I want to combine them into a single signal producer that produces a signal whenever one of them produces a signal.
首先, combineLatest
功能看起来是个不错的解决方案
At first the combineLatest
function looked like a good solution
let combinedProducer = combineLatest(signalProducer1, signalProducer2, signalProducer3)
但是,根据,生成的生产者只生成其第一个符号当这三个人都发出信号时。
However, according to this article, the resulting producer only produces its first signal when all the three have produced a signal.
显示我想要的内容,因此我想使用 .Merge $ c $使用
flatten
函数c> FlatteningStrategy。但是,我很难找到实现此目的的语法。
This interactive diagram shows exactly what I want, so I want to use the flatten
function with the .Merge
FlatteningStrategy. However, I'm having a hard time figuring out the syntax to achieve this.
推荐答案
您可以按如下方式实现:
You can achieve that as follows:
let merged = SignalProducer(values: [ signalProducer1, signalProducer2, signalProducer3 ])
|> flatten(.Merge)
这篇关于ReactiveCocoa将SignalProducers合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!