I have an array of Property<Int>
, and I need to reduce them to get sum of the last inputs (it's basically an unread notification counter from different SDK's) and put that into new Property<Int>
, I tried this
let unseen: Property<Int> = .init(
initial: 0,
then: countExtractor(counters: counters)
)
func countExtractor(counters: [Property<Int>]) -> SignalProducer<Int?, Never> {
SignalProducer { observer, lifetime in
guard !lifetime.hasEnded else {
return
}
let producers = counters.map { $0.producer }
lifetime += SignalProducer<SignalProducer<Int, Never>, Never>(producers)
.flatten(.latest)
.reduce(0) { previous, current in
return (previous ?? 0) + current
}
.start(observer)
}
}
And it's working, but the values are not updating (when I hard code only one property everything is working correctly)
The answer was so simple
let properties = Property<Int>.combineLatest(counters)
let count = properties?.producer.map {
$0.reduce(0, +)
}
guard let count = count else {
return SignalProducer(value: 0)
}
return count