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 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.
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:
Update: RAC 4.2.1 and upwards
Due to changes in how
flatten
works we need to help the compiler an be more explicit about the types:That becomes a bit cumbersome, so you might want to split it:
Thanks @Harry for the comment pointing the new version issue out.
RAC 4.2 and below
In RAC 4 this would be
At the moment Xcode 7.1.1 doesn't suggest
.flatten
in the autocompletion window, which might result in you (or just me) thinking it is not there, but if you type it all it will work.