5. Imperative
int x, y = 1, z = 1;
x = y + z; // x = 2; y = 1; z = 1;
y = 3; // x = 2; y = 3; z = 1;
NSLog(@"%d", x); // "2"
6. Reactive
int x, y = 1, z = 1;
x = y + z; // x = 2; y = 1; z = 1;
y = 3; // x = 4; y = 3; z = 1;
NSLog(@"%d", x); // "4"
7. We have this: KVO
// In your viewDidLoad/awakeFromNib/init
[self addObserver:self
forKeyPath: @"someString"
options:NSKeyValueObservingOptionNew
context:&someStringChangeContext];
// In dealloc
[self removeObserver: self
forKeyPath: @"someString"
context:&someStringChangeContext];
// Elsewhere in your class
- (void)observeValueForKeyPath:( NSString *)keyPath
ofObject:( id)object
change:( NSDictionary *)change
context:( void *)context
{
if (context == &someStringChangeContext) {
if ([keyPath isEqualToString: @"someString"]) {
// Do a bunch of stuff here
}
}
}
8. Do it in RAC
[RACAble(self.someString) distinctUntilChanged]
subscribeNext:^(NSString *string) {
// Do a bunch of things here, just like you would with KVO
}];
Source: http://tonyarnold.me/post/reactivelessons
9. Streams
A series of object values.
● Accessed sequentially.
● Available immediately or in the future.
● Allow for declarative transforms
Two types:
1. Signals
2. Sequences
12. Signal
A push-driven stream.
● Generally represents data that will be
delivered in the future.
● Values are sent on the signal
● Users subscribe to access values
3 types of events sent to subscribers:
1. Next - provides next value
2. Error
3. Completed