Search code examples
iosreactive-swift

How to trigger block from any of multiple signal producers?


How do I trigger one block of code whenever any of a set of SignalProducers change? In other words, how do I get rid of my current redundant code:

property1.producer.startWithValues { (value) in 
    // do stuff with property1.value and property2.value
}

property2.producer.startWithValues { (value) in 
    // do the same stuff with property1.value and property2.value
}

Solution

  • You can use combineLatest to create a new property that contains both values:

    let prop = property1.combineLatest(with: property2)
    prop.producer.startWithValues { (val1, val2) in
        // do stuff here
    }
    

    If either value changes, the block will be triggered.