Reactive Cocoa: Subscribe to New Values ​​Only

I am new to Reactive Cocoa. What I'm trying to achieve is notified every time the property value changes. However, I do not want to receive notifications when the property is set to the same value. Here is the code:

self.testProperty = 0;
[[RACObserve(self, self.testProperty) skip:1] subscribeNext:^(id x) {
    NSLog(@"Did Change: %@", x);
}];

self.testProperty = 1;
self.testProperty = 1;
self.testProperty = 1;
self.testProperty = 1;
self.testProperty = 1;

And this is what I get on the console output

> Did Change: 1
> Did Change: 1
> Did Change: 1
> Did Change: 1
> Did Change: 1

I expected Change to be printed only once, not five. Is there a way to subscribe only to new values?

+4
source share
1 answer

There is a distinctUntilChanged method for this :

[[[RACObserve(self, self.testProperty) 
  skip:1]
  distinctUntilChanged]
  subscribeNext:^(id x) {
    NSLog(@"Did Change: %@", x);
}];
+6
source

All Articles