9

If I'm using RACable like this:

[RACAbleWithStart(self.myProp) subscribeNext:^(id x) {
   // Do stuff

}];

How can can I access the old value of myProp (before the change the caused the signal to fire)? So I can access it like this:

[RACAbleWithStart(self.myProp) subscribeNext:^(id x) {
   // Do stuff
   id newValue = x;
   id oldValue = RAC_oldValue;
}];
zakdances
  • 22,285
  • 32
  • 102
  • 173
  • Why not just use KVO? Reactive Patterns deal with changes in x, not x. `RACAbleWithStart()` doesn't do what you think it does: It creates a new signal, but populates it with the initial value of x, instead of waiting for a change to fire. It doesn't remember anything, and it is not meant to give you "before and after" views of variable state. – CodaFi Apr 17 '13 at 21:47
  • @CodaFi I'm not expecting RACAbleWithStart to give me the old value. I know it just makes the subscriber fire immediately. I'm asking this question to check if there is a way to access the old value, since ReactiveCocoa is designed as a easier to use wrapper around KVO. – zakdances Apr 17 '13 at 22:01
  • 2
    Yes, it is a wrapper around KVO, but as I said, reactive patterns don't deal with old values, they deal with changes and new values. If you need anything close to saving an old value, run your RACAbleWithStart() relationship through a RACReplaySubject (it's not perfect, but it'll work). – CodaFi Apr 17 '13 at 23:52
  • @CodaFi Put that in an answer with an explanation/example and I'll green check it. – zakdances Apr 18 '13 at 01:13

1 Answers1

4

I have used this snippet with success:

[[object rac_valuesAndChangesForKeyPath:@"property" options:NSKeyValueObservingOptionOld observer:self] subscribeNext:^(RACTuple *tuple) {
    id newObject = tuple.first;
    NSDictionary *change = tuple.second;
    id oldObject = change[NSKeyValueChangeOldKey];
}];

Source: ReactiveCocoa documentation

Tomasz Bąk
  • 6,124
  • 3
  • 34
  • 48