How to trigger block from any of multiple signal producers?

一个人想着一个人 提交于 2019-12-12 04:43:52

问题


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
}

回答1:


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.




回答2:


You can save the block of code as a variable, then you would simply assign that variable to property1.producer.startWithValues.



来源:https://stackoverflow.com/questions/46372792/how-to-trigger-block-from-any-of-multiple-signal-producers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!