bjornorri bjornorri - 6 months ago 69
Swift Question

ReactiveCocoa combine SignalProducers into one

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.

At first the
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.

This interactive diagram shows exactly what I want, so I want to use the
function with the
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)