I have a Variable which is an array of enum values. These values change over time.
enum Option {
case One
case Two
case Three
}
let options = Variable<[Option]>([ .One, .Two, .Three ])
I then observe this variable for changes. The problem is, I need to know the diff between the newest value and the previous value. I'm currently doing this:
let previousOptions: [Option] = [ .One, .Two, .Three ]
...
options
.asObservable()
.subscribeNext { [unowned self] opts in
// Do some work diff'ing previousOptions and opt
// ....
self.previousOptions = opts
}
Is there something built in to RxSwift that would manage this better? Is there a way to always get the previous and current values from a signal?
there you go
options.asObservable()
.scan( [ [],[] ] ) { seed, newValue in
return [ seed[1], newValue ]
}
// optional, working with tuple of array is better than array of array
.map { array in (array[0], array[1]) }
//optional, in case you dont want empty array
.skipWhile { $0.count == 0 && $1.count == 0 }
it will return Observable<([Options], [Options])>
:)