I have a SignalProducer
that contains an array, I want to observe only new changes and not get the whole array when I observe it
I try flatMap .latest
but it does not work
Anyone have an idea of how to do that ?
There is no single function available that does that, but you can implement this by using combinePrevious
:
let change = producer.combinePrevious([]).map { (previous, current) in
// Calculate change
return change
}
How you calculate the change between the previous array and the current array, however, depends on what exactly "only new changes" means in your context.
Do you want the elements that have been added? Elements that have been removed? Are there duplicates allowed? Does the position matter and has the change to include the change in position?
If you only need the elements that have been added and have no duplicates, I suggest to use a Set
instead of an Array
E.g:
let p = MutableProperty([1, 2, 3])
let added = p.producer.map(Set.init)
.combinePrevious([])
.map { (previous, current) in
return current.subtracting(previous)
}
added.startWithValues { print($0) }
p.value = [1, 2, 3, 4]
p.value = [2, 4]
p.value = [1, 2, 3, 4, 5, 6]